LINQ 到 SQL - 分组依据

发布于 2024-12-09 19:30:10 字数 1583 浏览 0 评论 0原文

我有以下 LINQ-to-SQL 代码

var customerList = from cm in dc.ConsignmentMarginBreakdowns
                   join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
                   join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
                   join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
                   where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
                   where tm.Depot == depotLetter
                   group cm by new { ss.Name, ss.ID } into cmg
                   select new
                   {
                       CustomerID = cmg.Key.ID,
                       CustomerName = cmg.Key.Name,
                       Sales1 = cmg.Where(a => a.DeliveryDate >= dateRange1.FromDate && a.DeliveryDate <= dateRange1.ToDate).Sum(a => a.TripSalesTotal),
                       Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.DeliveryDate >= dateRange2.FromDate && a.DeliveryDate <= dateRange2.ToDate).Sum(a => a.TripSalesTotal) : 0.00m
                   };

我遇到的问题是,当我选择时,“tm”中的“DeliveryDate”不在查询范围内。有什么想法可以在我的查询中获取这个值吗?

谢谢

I have the following LINQ-to-SQL code

var customerList = from cm in dc.ConsignmentMarginBreakdowns
                   join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
                   join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
                   join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
                   where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
                   where tm.Depot == depotLetter
                   group cm by new { ss.Name, ss.ID } into cmg
                   select new
                   {
                       CustomerID = cmg.Key.ID,
                       CustomerName = cmg.Key.Name,
                       Sales1 = cmg.Where(a => a.DeliveryDate >= dateRange1.FromDate && a.DeliveryDate <= dateRange1.ToDate).Sum(a => a.TripSalesTotal),
                       Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.DeliveryDate >= dateRange2.FromDate && a.DeliveryDate <= dateRange2.ToDate).Sum(a => a.TripSalesTotal) : 0.00m
                   };

The problem I have is that the 'DeliveryDate' from 'tm' is not in the scope of the query when I select. Any ideas how I can get this value in my query?

Thanks

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

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

发布评论

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

评论(1

感性 2024-12-16 19:30:10

只需创建另一个匿名类型

 group new { cm, tm } by new { ss.Name, ss.ID } into cmg

,这样您的查询将如下所示:

var customerList = from cm in dc.ConsignmentMarginBreakdowns
                   join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
                   join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
                   join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
                   where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
                   where tm.Depot == depotLetter
                   group new { cm, tm } by new { ss.Name, ss.ID } into cmg
                   select new
                   {
                       CustomerID = cmg.Key.ID,
                       CustomerName = cmg.Key.Name,
                       Sales1 = cmg.Where(a => a.tm.DeliveryDate >= dateRange1.FromDate && a.tm.DeliveryDate <= dateRange1.ToDate).Sum(a => a.cm.TripSalesTotal),
                       Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.tm.DeliveryDate >= dateRange2.FromDate && a.tm.DeliveryDate <= dateRange2.ToDate).Sum(a => a.cm.TripSalesTotal) : 0.00m
                   };

Just create another anonymous type

 group new { cm, tm } by new { ss.Name, ss.ID } into cmg

so your query would look like this:

var customerList = from cm in dc.ConsignmentMarginBreakdowns
                   join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
                   join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
                   join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
                   where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
                   where tm.Depot == depotLetter
                   group new { cm, tm } by new { ss.Name, ss.ID } into cmg
                   select new
                   {
                       CustomerID = cmg.Key.ID,
                       CustomerName = cmg.Key.Name,
                       Sales1 = cmg.Where(a => a.tm.DeliveryDate >= dateRange1.FromDate && a.tm.DeliveryDate <= dateRange1.ToDate).Sum(a => a.cm.TripSalesTotal),
                       Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.tm.DeliveryDate >= dateRange2.FromDate && a.tm.DeliveryDate <= dateRange2.ToDate).Sum(a => a.cm.TripSalesTotal) : 0.00m
                   };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文