【问题标题】:How to get all the rows of a table, where a column's value occurs more than once in LINQ to Entities?如何获取表的所有行,其中列的值在 LINQ to Entities 中多次出现?
【发布时间】: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


    【解决方案1】:

    使用分组

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多