如何从linq结果中删除specefic行

时间:2015-11-15 13:55:46

标签: c# linq

我有这个Linq to Entity:

        var sitesGrpByClientAndFreq = from cfr in clientFullReview2                                           
                                       group cfr by new { cfr.inspectionFreqvency } into g
                                       select new
                                       {
                                           inspectionFreqvency = g.Key.inspectionFreqvency,
                                           siteCount = g.Select(x => x.siteId).ToArray().Distinct().Count(),
                                           normalSitesCount = g.Where(x => x.isNormal == true).Select(x=>x.isNormal).ToArray().Count(),
                                       }

从上面的linq我得到了这个结果:

enter image description here

我的问题是如何删除inspectionFrequency = -1的所有行?

1 个答案:

答案 0 :(得分:1)

您可以将let关键字与where混合搭配。它可能是这样的:

var sitesGrpByClientAndFreq =
  from cfr in clientFullReview2                                           
   group cfr by new { cfr.inspectionFreqvency } into g
   let inspectionFreqvency = g.Key.inspectionFreqvency
   where inspectionFreqvency != -1
   select new
   {
       inspectionFreqvency = inspectionFreqvency,
       siteCount = g.Select(x => x.siteId).ToArray().Distinct().Count(),
       normalSitesCount = g.Where(x => x.isNormal == true).Select(x=>x.isNormal).ToArray().Count(),
   }