SQL查询将其转换为Linq

时间:2017-06-07 16:54:39

标签: sql entity-framework linq

select 
    COUNT(prosold.Product_ID) as c,
    prosold.Product_ID,
    ProImg.ImagePath 
from 
    ProductSold as ProSold,
    ProductImages as ProImg  
where 
    ProSold.Product_ID = ProImg.Product_ID  
group by 
    ProSold.Product_ID, ProImg.ImagePath 
order by 
    c desc

我这样做但最终没有工作

var result = from p in db.Products
             join c in db.ProductSolds on p.Product_ID equals c.Product_ID into j1
             from j2 in j1.DefaultIfEmpty()
             group j2 by p.Product_ID into grouped
             select new { ParentId = grouped.Key, Count = grouped.Count(t => t.Product_ID != null) };

1 个答案:

答案 0 :(得分:1)

var ans = from ProSold in ProductSold
          join ProImg in ProductImages on ProSold.Product_ID equals ProImg.Product_ID
          group new { ProSold, ProImg } by new { ProSold.Product_ID, ProImg.ImagePath } into ppg
          let c = ppg.Count()
          orderby ppg descending
          select new { c, ppg.Key.Product_ID, ppg.Key.ImagePath };
相关问题