简体   繁体   English

如何在Linq中进行分组

[英]Howto do a group by and having in Linq

After watching a couple of questions about howto create a linq query that has a groupby and where, i tried it myself but had no luck. 观看了几个有关如何创建具有groupby和位置的linq查询的问题后,我自己尝试了一下,但是没有运气。

I want to convert this SQL query to a corresponding linq query 我想将此SQL查询转换为相应的linq查询

select distinct city from zipcodedata 
group by city
having city like 'zw%'

This query is ready in about 0,03 seconds on my development machine with 8mln records in the table. 在表上有8mln记录的开发机器上,此查询大约在0,03秒内即可完成。 But when i try to create aa linq query like this: 但是当我尝试创建像这样的linq查询时:

res = _context.Zipcodes
            .GroupBy(z => z.City)
            .Where(z => z.Key.Contains(query))
            .Select(z => z.Key)
            .ToList();

The sql profiler tells me the following query sql profiler告诉我以下查询

SELECT [z].[Zipcode], [z].[Street], [z].[Streetnumber], [z].[City], [z]. [Latitude], [z].[Longitude], [z].[Municipality], [z].[Province]
FROM [ZipcodeData] AS [z]
ORDER BY [z].[City]

I dont see a where nor a having, can some explain me why this happens. 我看不到那里也没有,有人可以解释我为什么会这样。

as a footnote im using Dotnet CORE 1.1.0 with EFCore 1.1.0 作为脚注即时消息使用Dotnet CORE 1.1.0和EFCore 1.1.0

After further investigation i found out that it is not yet possible in EF-Core according to the Feature Comparison between EF-Core and EF 经过进一步研究,根据EF-Core与EF之间功能比较,我发现EF-Core中尚不可能

It states Features not in EF Core --> Querying Data : 它声明Features not in EF Core --> Querying Data

GroupBy translation in particular will move translation of the LINQ GroupBy operator to the database, rather than in-memory. 特别是GroupBy转换会将LINQ GroupBy运算符的转换移到数据库而不是内存中。

See this issue on Github exactly what i see when looking at the query in SQL Profiler :-( 在Github上看到此问题 ,与在SQL Profiler中查询时看到的完全一样:-(

I hope other people will find this answer useful 我希望其他人会发现这个答案有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM