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.
解决方法
.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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。