通过min,count将SQL转换为LINQ组

时间:2012-04-28 04:34:01

标签: linq

如何将SQL转换为LINQ

SELECT 
          [good_id]
          ,MIN([good_price]) as minPrice
          ,Count([distributor_id]) as distrCount
      FROM [Provizor].[dbo].[PRICES] where region_id=22
      GROUP BY [good_id]
      ORDER BY distrCount desc 

如何在LINQ分组中执行此操作

1 个答案:

答案 0 :(得分:3)

这样的事情:

var query = dbo.Prices
               .Where(x => x.region_id == 22)
               .GroupBy(x => x.good_id)
               .Select(g => new 
                { 
                   minPrice = g.Min(x => x.good_price), 
                   distrCount = g.Count(x=> x.distributor_id!=null)
                }
               .OrderByDescending(x => x.distrCount);