如何将此 sql 查询转换为 linq 查询。我被困在 group by 和 sum 部分
How to convert this sql query into linq query. I am stuck at the group by and sum part
我的 SQL 查询看起来像这样:
SQL 查询 returns 与存在的每个 colorwaysid 关联的收入
select p.colorwaysid,sum(oivs.actItemvalue+oivs.custom) as revenue
from Product p inner join eshakti_corp..orderitemvaluesplit oivs on p.productid = oivs.productid
inner join eshakti_corp..orders o on oivs.orderid = o.orderid
where colorwaysid is not null and o.crdate >= '4/1/2022' and o.crdate <= '4/30/2022'
group by p.colorwaysid order by revenue desc
到目前为止,我已经在 LINQ C# 中完成了此操作
from product in eshaktiDb.Products
join oivs in corpDb.OrderitemvalueSplits on product.ProductId equals oivs.Productid
join order in corpDb.Orders on oivs.Orderid equals order.OrderID
where product.ColorWaysId != null && order.CrDate >= fromDate && order.CrDate <= toDate
请帮助我创建完整的 Linq 查询。
谢谢!
group by
部分很明显:
var query =
from product in eshaktiDb.Products
join oivs in corpDb.OrderitemvalueSplits on product.ProductId equals oivs.Productid
join order in corpDb.Orders on oivs.Orderid equals order.OrderID
where product.ColorWaysId != null && order.CrDate >= fromDate && order.CrDate <= toDate
group ovis by p.colorwaysid into g
select new
{
colorwaysid = g.Key,
revenue = g.Sum(o => o.actItemvalue + o.custom)
};
请注意,如果您正确配置了导航属性,则可以省略连接。
我的 SQL 查询看起来像这样: SQL 查询 returns 与存在的每个 colorwaysid 关联的收入
select p.colorwaysid,sum(oivs.actItemvalue+oivs.custom) as revenue
from Product p inner join eshakti_corp..orderitemvaluesplit oivs on p.productid = oivs.productid
inner join eshakti_corp..orders o on oivs.orderid = o.orderid
where colorwaysid is not null and o.crdate >= '4/1/2022' and o.crdate <= '4/30/2022'
group by p.colorwaysid order by revenue desc
到目前为止,我已经在 LINQ C# 中完成了此操作
from product in eshaktiDb.Products
join oivs in corpDb.OrderitemvalueSplits on product.ProductId equals oivs.Productid
join order in corpDb.Orders on oivs.Orderid equals order.OrderID
where product.ColorWaysId != null && order.CrDate >= fromDate && order.CrDate <= toDate
请帮助我创建完整的 Linq 查询。 谢谢!
group by
部分很明显:
var query =
from product in eshaktiDb.Products
join oivs in corpDb.OrderitemvalueSplits on product.ProductId equals oivs.Productid
join order in corpDb.Orders on oivs.Orderid equals order.OrderID
where product.ColorWaysId != null && order.CrDate >= fromDate && order.CrDate <= toDate
group ovis by p.colorwaysid into g
select new
{
colorwaysid = g.Key,
revenue = g.Sum(o => o.actItemvalue + o.custom)
};
请注意,如果您正确配置了导航属性,则可以省略连接。