加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq选择在内存中执行的不同计数

发布时间:2020-12-15 22:53:03 所属栏目:百科 来源:网络整理
导读:我正在努力了解LINQ如何转换为SQL. 我有以下查询,我正在尝试使用LINQ生成. SELECT [OrganizationId],[DepartmentId],[LocationName],[Group1],[Group2],[Group3],[BooklistId],[BooklistName],COUNT(DISTINCT [OrdererId]),MAX([ExpectedDate])FROM [Orders]
我正在努力了解LINQ如何转换为SQL.

我有以下查询,我正在尝试使用LINQ生成.

SELECT [OrganizationId],[DepartmentId],[LocationName],[Group1],[Group2],[Group3],[BooklistId],[BooklistName],COUNT(DISTINCT [OrdererId]),MAX([ExpectedDate])
FROM [Orders]
WHERE ([DepartmentId] IS NOT NULL AND ([DepartmentId] = '<Guid>')) AND ([Schoolyear] = '2018')
GROUP BY [OrganizationId],[BooklistName]
ORDER BY [BooklistName]

使用索引,此查询的执行时间低于200毫秒

我的LINQ查询如下:

await _context
            .Orders
            .Where(i => i.DepartmentId != null && i.DepartmentId.Equals(Parameters.DepartmentId))
            .Where(i => i.SchoolYear.Equals(Parameters.SchoolYear))
            // Group the data.
            .GroupBy(orders => new
            {
                orders.BooklistId,orders.BooklistName,orders.OrganizationId,orders.DepartmentId,orders.LocationName,orders.Groep1,orders.Groep2,orders.Groep3
            })
            .OrderBy(i => i.Key.BooklistName)
            .Select(i => new BookListViewModel
            {
                Count = i.Select(orders => orders.OrdererId).Distinct().Count(s => s != null),Id = i.Key.OrganizationId,Name = i.Key.BooklistName,LocationName = i.Key.LocationName,Number = i.Key.BooklistId,Group1 = i.Key.Group1,Group2 = i.Key.Group2,Group3 = i.Key.Group3,DepartmentId = i.Key.DepartmentId,ExpectedDate = i.Max(orders => orders.ExpectedDate)
            })
            .ToListAsync();

但是我一直看到:

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression ‘GroupBy(new <>f__AnonymousType1`8(BooklistId = [i].BooklistId,BooklistName = [i].BooklistName,OrganizationId = [i].OrganizationId,DepartmentId = [i].DepartmentId,LocationName = [i].LocationName,Group1 = [i].Group1,Group2 = [i].Group2,Group3 = [i].Group3),[i])’ could not be translated and will be evaluated locally.

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression ‘Distinct()’ could not be translated and will be evaluated locally.

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression ‘where ([s] != null)’ could not be translated and will be evaluated locally.

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression ‘Count()’ could not be translated and will be evaluated locally.

任何人都可以告诉我为什么LINQ查询在内存中执行?我需要在LINQ查询中更改以获得我想要的结果?

解决方法

查询正在内存中执行,因为您使用该语句实例化了BookListViewModel对象的集合

.Select(i => new BookListViewModel
            {...})

如果您只是删除类BookListViewModel,Linq将执行在db端执行查询(这是一个好主意,因为优化器更有效),像这样……

.Select(i => new
            {
                Count = i.Select(orders => orders.OrdererId).Distinct().Count(s => s != null),i.Key.OrganizationId,i.Key.BooklistName,i.Key.LocationName,i.Key.BooklistId,i.Key.Group1,i.Key.Group2,i.Key.Group3,i.Key.DepartmentId,ExpectedDate = i.Max(orders => orders.ExpectedDate)
            })

然后你可以在最后实例化你的集合,所以整个事情看起来像这样……

await _context
            .Orders
            .Where(i => i.DepartmentId != null && i.DepartmentId.Equals(Parameters.DepartmentId))
            .Where(i => i.SchoolYear.Equals(Parameters.SchoolYear))
            // Group the data.
            .GroupBy(orders => new
            {
                orders.BooklistId,orders.Group1,orders.Group2,orders.Group3
            })
            .OrderBy(i => i.Key.BooklistName)
.Select(i => new
            {
                Count = i.Select(orders => orders.OrdererId).Distinct().Count(s => s != null),ExpectedDate = i.Max(orders => orders.ExpectedDate)
            })
            .Select(i => new BookListViewModel
            {
                Count = i.Count,Id = i.Id,Name = i.Name,LocationName = i.LocationName,Number = i.Number,Group1 = i.Group1,Group2 = i.Group2,Group3 = i.Group3,DepartmentId = i.DepartmentId,ExpectedDate = i.ExpectedDate
            })
            .ToListAsync();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读