在C#中将具有Group by和Haveing子句的Sql查询转换为Linq To Sql查询 [英] Convert Sql query with Group by and Having clause to Linq To Sql query in C#
本文介绍了在C#中将具有Group by和Haveing子句的Sql查询转换为Linq To Sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助将以下ql查询转换为Linq转换为Sql查询.
I need help to convert following ql query to Linq to Sql query.
select Name, Address
from Entity
group by Name, Address
having count(distinct LinkedTo) = 1
想法是查找仅具有1个不同的LinkedTo值的所有唯一的名称,地址"对.请记住,表中还有其他列.
Idea is to find all unique Name, Address pairs who only have 1 distinct LinkedTo value. Remember that there are other columns in the table as well.
推荐答案
我会尝试这样的事情:
Entity.GroupBy(e => new { e.Name, e.Address})
.Where(g => g.Select(e => e.LinkedTo).Distinct().Count() == 1)
.Select(g => g.Key);
您应该在该行之后放置一个断点,并检查所生成的SQL,以查找真正进入数据库的内容.
You should put a breakpoint after that line and check the SQL that is generated to find what is really going to the database.
这篇关于在C#中将具有Group by和Haveing子句的Sql查询转换为Linq To Sql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文