LINQ 到 SQL - 分组依据
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需创建另一个匿名类型
,这样您的查询将如下所示:
Just create another anonymous type
so your query would look like this: