将SQL查询转换为linq查询表达式

时间:2015-10-25 08:49:07

标签: c# linq

我有一个SQL查询,我试图在C#中转换为LINQ查询表达式,但我无法正确执行。

查询如下:

select 
    P.id, P.name, COUNT(S.orderqty)
from 
    product as P 
inner join 
    sales as S on P.id = S.id
group by 
    p.id, p.name

我的linq代码:

IQueryable<ReportType1> query = 
    from product in EngineContext.Products
    join SalesOrder inEngineContext.SalesOrderDetails  
    on product.ProductID equals SalesOrder.ProductID
    group SalesOrder.OrderQty by product into groups
    select new ReportType1() {    
        Year = null,
        ProducId = groups.Key.ProductID,
        Name = groups.Key.Name,
        Quantity = ??
    };

Quantity的价值写什么?

1 个答案:

答案 0 :(得分:1)

小组计数方法:

select new ReportType1
{
    Year = null,
    ProducId = groups.Key.ProductID,
    Name = groups.Key.Name,
    Quantity = groups.Count()
}