在使用闭包的 LINQ 语句中,是否可以在闭包内指定 where 子句?
这是我试图解决的问题。我有 3 个数据库表 - Sales、Customer 和 Time。一份销售记录恰好链接到一份客户记录和一份时间记录。我只想查看 1996 年按地区(客户与特定地区相关)的总销售额,并进一步细分:按节假日、非节假日、工作日和周末。这是我迄今为止粗略的查询,以及我试图在评论中添加的内容。
var totalSales =
from s in sales
where s.Time.Year = 1996
group s by s.Customer.Region into g
select new { Region = g.Key,
Holidays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Holiday_flag = true)
NonHolidays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Holiday_flag = false)
Weekdays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Weekday_flag = true)
Weekends = g.Sum(s => s.Total_Amount)}; // WHERE (s => s.Time.Weekday_flag = false)
为此,我需要能够在每个闭包内进一步限制结果。这可能吗?我需要重构查询吗?当然,我可以通过将其分成 4 个单独的查询来完成此任务,但如果能在一个查询中完成它,那就太好了。
谢谢。
Here is the problem I am attempting to solve. I have 3 database tables - Sales, Customer, and Time. One sales record links to exactly one customer record, and one time record. I want to, for the year 1996 only, look at the total sales by region (a customer is tied to a specific region), for the following further divisions: by holidays, non-holidays, weekdays, and weekends. Here is the query I have so far roughly, with what I am trying to add in comments.
var totalSales =
from s in sales
where s.Time.Year = 1996
group s by s.Customer.Region into g
select new { Region = g.Key,
Holidays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Holiday_flag = true)
NonHolidays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Holiday_flag = false)
Weekdays = g.Sum(s => s.Total_Amount), // WHERE (s => s.Time.Weekday_flag = true)
Weekends = g.Sum(s => s.Total_Amount)}; // WHERE (s => s.Time.Weekday_flag = false)
To do this, I would need to be able to, within each closure, further limit the results. Is this possible? Do I need to restructure the query? I could of course accomplish this by breaking it apart into 4 separate queries, but it would be really nice to do it in one.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,你可以尝试这个:
或者:
Well, you could try this:
or: