如何在LINQ查询中显示重复的记录重复两次或少于两次

时间:2019-03-14 11:09:06

标签: entity-framework linq

我有一张这样的桌子:

---------------------------------------
|       Name       |       code       |     
---------------------------------------
|       Sam                01         |
|       Sam                01         |
|       Sam                01         |
|       Dani               02         |
|       Dani               02         |
|       Vector             03         |
|       vector             03         |
|       vector             03         |
|       Sara               04         |
---------------------------------------

我需要显示重复的记录,例如2次或少于2次

result : (Records <= 2)

    Dani               02 
    Dani               02
    Sara               04

我的LINQ查询从两个表和条件中选择:

var result = (from ArdSamanehs in _entity.ArdSamanehs
           join TableExcels in _entity.TableExcels
           on new { ArdSamanehs.Cardex, DorehNo = 
           (string)ArdSamanehs.DorehNo }
           equals new { Cardex = TableExcels.CardexN, DorehNo = "12" }
                      group new { ArdSamanehs, TableExcels } by new
                      {
                          ArdSamanehs.NameFamily,
                          ArdSamanehs.Doreh,
                          ArdSamanehs.CityDesc,
                          TableExcels.CardexN,
                          ArdSamanehs.Cardex,
                          ArdSamanehs.Date
                      } into g

                      orderby
                        g.Key.NameFamily

                      select new
                      {
                          g.Key.NameFamily,
                          g.Key.Doreh,
                          g.Key.CityDesc,
                          g.Key.Date
                      }).ToList();

如何使用c#LINQ查询?

1 个答案:

答案 0 :(得分:2)

请尝试这个。

  var lists = result.GroupBy(x => new { x.Name, x.Code})
                              .Where(x => x.Count() <= 2).SelectMany(x => x)
                              .ToList();