public virtual int Count(Search search)
 {
     if (search == null)
         throw new ArgumentNullException("search");
     var query = this.GetQuery(search);
     return query.Count();
 }
 public virtual Paging<CommandLog> Select(Search search, int page, int size)
 {
     if (search == null)
         throw new ArgumentNullException("search");
     var query = this.GetQuery(search);
     query = query.OrderByDescending(l => l.CreateTime);
     var metas = new Paging<CommandLogMeta>(query, page, size);
     var logs = Mapper.Map<CommandLogMeta[], CommandLog[]>(metas.Data);
     return new Paging<CommandLog>(logs, metas.PageCount, metas.TotalCount);
 }
 /// <summary>
 /// 根据搜索条件生成查询
 /// </summary>
 /// <param name="search"></param>
 /// <returns></returns>
 public IQueryable<DeviceLogMeta> GetQuery(Search search)
 {
     var query = this._logData.Table;
     //主键条件
     if (search.Ids != null && search.Ids.Length > 0)
         query = query.Where(l => search.Ids.Contains(l.Id));
     //时间段开始条件
     if (search.StartTime.HasValue)
         query = query.Where(l => l.CreateTime >= search.StartTime.Value);
     //时间段结束条件
     if (search.EndTime.HasValue)
         query = query.Where(l => l.CreateTime <= search.EndTime.Value);
     //是否可用条件
     if (search.Enabled.HasValue)
         query = query.Where(l => l.Enabled == search.Enabled.Value);
     //状态条件
     if(search.Status!=null&&search.Status.Length>0)
     {
         var selector = new List<string>();
         var arr = new object[search.Status.Length];
         for (var i = 0; i < search.Status.Length; i++)
         {
             selector.Add(string.Format("Status=@{0}", i));
             arr[i] = search.Status[i];
         }
         query = query.Where(string.Join(" OR ", selector.ToArray()), arr);
     }
     //设备条件
     if (search.Devices != null && search.Devices.Length > 0)
     {
         var whereIndex = -1;
         var selector = new List<string>();
         var arr = new object[search.Devices.Length * 2];
         var arrIndex = -1;
         foreach (var device in search.Devices)
         {
             selector.Add(string.Format("(DeviceId=@{0} AND DeviceType=@{1})", ++whereIndex, ++whereIndex));
             arr[++arrIndex] = device.Id;
             arr[++arrIndex] = device.Type;
         }
         query = query.Where(string.Join(" OR ", selector.ToArray()), arr);
     }
     return query;
 }