【发布时间】:2013-06-28 13:37:45
【问题描述】:
我在 SQL Server 中有表“客户”,它有 3 列:“Col1”、“Col2”、“Col3”。
我想获取 Col2 值出现多次的所有行。
这是我的查询:
Context.Customers.Where(x => x.Col2.Count() > 1).ToList();
但它不起作用。你有什么想法吗?
谢谢
【问题讨论】:
标签: c# linq linq-to-entities
我在 SQL Server 中有表“客户”,它有 3 列:“Col1”、“Col2”、“Col3”。
我想获取 Col2 值出现多次的所有行。
这是我的查询:
Context.Customers.Where(x => x.Col2.Count() > 1).ToList();
但它不起作用。你有什么想法吗?
谢谢
【问题讨论】:
标签: c# linq linq-to-entities
使用分组
Context.Customers.GroupBy(x => x.Col2) // group by Col2 value
.Where(g => g.Count() > 1) // get groups with more than one item
.SelectMany(g => g) // flatten results
.ToList();
【讨论】: