使用 linq 从不同表中选择总和列

发布于 2024-10-28 03:51:55 字数 1345 浏览 2 评论 0原文

这是我的 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),
Shipping = g.Sum(o => o.ShippingPrice),
Tax = g.Sum(o => o.Tax),
<-- these make error...

what should I do!!!
please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2024-11-04 03:51:55

可以

group new { Item = oi, Order = o } by new {o.StoreID, o.CreatedOn.Year, o.CreatedOn.Month, StoreNmae="Test" } into g

然后你

g.Sum(o => o.Order.Total)

求和

g.Sum(o => o.Item.Quantity)

You could

group new { Item = oi, Order = o } by new {o.StoreID, o.CreatedOn.Year, o.CreatedOn.Month, StoreNmae="Test" } into g

then you could sum

g.Sum(o => o.Order.Total)

and

g.Sum(o => o.Item.Quantity)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文