我有一个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
的价值写什么?
答案 0 :(得分:1)
小组计数方法:
select new ReportType1
{
Year = null,
ProducId = groups.Key.ProductID,
Name = groups.Key.Name,
Quantity = groups.Count()
}