EF Core 2.1通过跳过进行分组

时间:2019-01-03 17:50:30

标签: c# linq c#-6.0 ef-core-2.1

我当前正在通过IQueryable进行“分页”,该IQueryable具有许多字段{Table1Id,Table1Label,IsActive,...},并且在IQueryable结果中可以有多个记录,用于PdcId,如下所示:

var result = from a in _entities.table1
             join b in _entities.table2 on new { a.Table1Id, a.Table1LevelId } equals new { Table1Id = b.Table2Id, Table1LevelId = b.Table2LevelId }                
             join c in _entities.Table3 on a.Table1Id equals c.Table1Id into cc from ccc in cc.DefaultIfEmpty()            
             where a.Valide == true 
             select new MeteringPointDetailModel
             {
                Table1Id = a.Table1Id,
                Table1Label = a.Label,
                IsActive = a.IsActive,                                     
                Table3Label = ccc.Label,                 
             };

我尝试了这段代码,但是当我看到使用sql profiler在sql中生成结果时,查询正在运行,而且没有停止! (生成多个sql查询)。

var data = await result.Where(i => result.GroupBy(p => p.Table1Id)
                       .Select(t => t.Key)
                       .Skip(query.Page).Take(query.PageSize)
                       .Contains(i.Table1Id)).ToListAsync();

如果我想先按Table1Id分组然后跳过并采用不同的Table1Id,该怎么办。

例如:如果我的结果有两个记录,一个结果是#50,另一个结果是51,如果我按50翻页,则不会得到记录51,而我想要它。

1 个答案:

答案 0 :(得分:0)

这就是我为我服务的工作:

解决方案1 ​​

var dataTable1 = (from a in _entities.table1 
                  where a.Valide == true 
                  select new {...}).Skip(query.Page).Take(query.PageSize);

var result = from a in dataTable1
         join b in _entities.table2 on new { a.Table1Id, a.Table1LevelId } equals new { Table1Id = b.Table2Id, Table1LevelId = b.Table2LevelId }                
         join c in _entities.Table3 on a.Table1Id equals c.Table1Id into cc from ccc in cc.DefaultIfEmpty()                         
         select new MeteringPointDetailModel
         {
            Table1Id = a.Table1Id,
            Table1Label = a.Label,
            IsActive = a.IsActive,                                     
            Table3Label = ccc.Label,                 
         };

var data = await result.ToListAsync();

解决方案2

var result = from a in _entities.table1
         join b in _entities.table2 on new { a.Table1Id, a.Table1LevelId } equals new { Table1Id = b.Table2Id, Table1LevelId = b.Table2LevelId }                
         join c in _entities.Table3 on a.Table1Id equals c.Table1Id into cc from ccc in cc.DefaultIfEmpty()            
         where a.Valide == true 
         select new MeteringPointDetailModel
         {
            Table1Id = a.Table1Id,
            Table1Label = a.Label,
            IsActive = a.IsActive,                                     
            Table3Label = ccc.Label,                 
         };

result = !string.IsNullOrWhiteSpace(query.Table3Label) ? result.Where(c => c.Table3Label.ToLower().Contains(query.Table3Label.ToLower())) : result;

var table1Ids = await result.OrderBy(p => p.Table1Id)
                            .Select(p => p.Table1Id).Distinct()
                            .Skip(query.Page).Take(query.PageSize)
                            .ToListAsync();

var data = await result.Where(p => table1Ids.Contains(p.Table1Id)).ToListAsync();

如果示例中指定的联接表中存在子句条件(搜索条件),则解决方案2更为有用。

希望它将对某人有所帮助。