以下SQL查询的LINQ等价物是什么?

时间:2011-11-26 09:33:42

标签: sql-server-2008 linq-to-sql .net-4.0

These are the 3 tables used here

T-SQL查询:

select 
   PRODUCT_ID, PRODUCT_NAME.NAME, SUM(SALES_PRICE) as PRODUCT_SALES 
from SALES_PRODUCT_DETAILS
join PRODUCT_NAME on PRODUCT_NAME.ID = SALES_PRODUCT_DETAILS.PRODUCT_ID
where 
    SALES_ID in 
        (select SALES_ID 
         from SALES 
         where SALES_DATE >= '2011-11-13' and SALES_DATE <= '2011-11-16')
group by 
    PRODUCT_ID, PRODUCT_NAME.NAME

对此,等效的LINQ查询是什么?

1 个答案:

答案 0 :(得分:1)

我认为这样的事情可以解决问题。

from s in SALES_PRODUCT_DETAILS
join p in PRODUCT_NAME on s.PRODUCT_ID equals p.ID
where SALES.Any(sales => 
             sales.SALES_DATE >= new DateTime(2011, 11, 13) && 
             sales.SALES_DATE <= new DateTime(2011, 11, 16) &&
             sales.SALES_ID == s.SALES_ID)
group s by new { s.PRODUCT_ID, p.NAME } into g
select new
{
   g.Key.Name,
   g.Key.PRODUCT_ID,
   PRODUCT_SALES = g.Sum(x => x.SALES_PRICE)
}