【问题标题】:convert SQL to LINQ group by min, count [closed]按分钟将 SQL 转换为 LINQ 组,计数 [关闭]
【发布时间】:2012-05-08 18:58:59
【问题描述】:

如何将 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 分组中执行此操作

【问题讨论】:

标签: linq


【解决方案1】:

类似这样的:

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

【讨论】:

  • LINQ var price = (from p in _db.PRICEs where p.region_id == 22 group p by p.good_id into g select new { good_id = g.Key ,minPrice = g.Min(x => x.good_price) , distrCount = g.Count(x => x.distributor_id != null) } ).OrderByDescending(x => x.distrCount);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 2021-01-28
  • 2021-01-19
  • 1970-01-01
相关资源
最近更新 更多