使用 linq 从不同表中选择总和列
这是我的 linq 代码..
( from Order o in masterRepository.Orders.Where(x => x.IsDeleted == false)
join OrderItem oi in masterRepository.OrderItems on o.OrderID equals oi.OrderID
group oi by new {o.StoreID, o.CreatedOn.Year, o.CreatedOn.Month, StoreNmae="Test" } into g
select new ST_SalesOrder
{
Year = g.Key.Year,
Month = g.Key.Month,
StoreID = g.Key.StoreID,
StoreName = "SA",
SubTotal = g.Sum(o => o.SubTotal),
Shipping = g.Sum(o => o.ShippingPrice),
Tax = g.Sum(o => o.Tax),
TotalAmount = g.Sum(o => o.Total),
TotalCount = g.Sum(oi => oi.Quantity)
}).OrderBy("Year desc, Month desc, StoreName").ToList();
问题是.. 我需要从 Order 表和 OrderItem 表获取总数据。 但我只能使用一张表进行分组。
如果我将 o by .... 分组,则 TotalCount = g.Sum(oi => oi.Quantity)
<-- 这会出错, 如果我将 oi 分组为...,则 SubTotal = g.Sum(o => o.SubTotal), 运费 = g.Sum(o => o.ShippingPrice), Tax = g.Sum(o => o.Tax), <-- 这些会出错......
我该怎么办! 请帮忙。
Here is my linq code..
( from Order o in masterRepository.Orders.Where(x => x.IsDeleted == false)
join OrderItem oi in masterRepository.OrderItems on o.OrderID equals oi.OrderID
group oi by new {o.StoreID, o.CreatedOn.Year, o.CreatedOn.Month, StoreNmae="Test" } into g
select new ST_SalesOrder
{
Year = g.Key.Year,
Month = g.Key.Month,
StoreID = g.Key.StoreID,
StoreName = "SA",
SubTotal = g.Sum(o => o.SubTotal),
Shipping = g.Sum(o => o.ShippingPrice),
Tax = g.Sum(o => o.Tax),
TotalAmount = g.Sum(o => o.Total),
TotalCount = g.Sum(oi => oi.Quantity)
}).OrderBy("Year desc, Month desc, StoreName").ToList();
The thing is that..
I need to get sum data from Order table and OrderItem table.
but I can only group by using one table.
If I group o by.... then TotalCount = g.Sum(oi => oi.Quantity)
<-- this makes error,
if I group oi by... then SubTotal = g.Sum(o => o.SubTotal),
<-- these make error...
Shipping = g.Sum(o => o.ShippingPrice),
Tax = g.Sum(o => o.Tax),
what should I do!!!
please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以
然后你
求和
You could
then you could sum
and