在使用闭包的 LINQ 语句中,是否可以在闭包内指定 where 子句?

发布于 2024-10-20 01:35:36 字数 856 浏览 1 评论 0原文

这是我试图解决的问题。我有 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 技术交流群。

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

发布评论

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

评论(1

梦在深巷 2024-10-27 01:35:36

好吧,你可以尝试这个:

Holidays = g.Where(s => s.Time.Holiday_flag).Sum(s => s.Total_Amount),
NonHolidays = g.Where(s => !s.Time.Holiday_flag).Sum(s => s.Total_Amount),
Weekdays = g.Where(s => s.Time.Weekday_flag).Sum(s => s.Total_Amount),
Weekends = g.Where(s => !s.Time.Weekday_flag).Sum(s => s.Total_Amount)

或者:

Holidays = g.Sum(s => s.Time.Holiday_flag ? s.Total_Amount : 0),
// etc

Well, you could try this:

Holidays = g.Where(s => s.Time.Holiday_flag).Sum(s => s.Total_Amount),
NonHolidays = g.Where(s => !s.Time.Holiday_flag).Sum(s => s.Total_Amount),
Weekdays = g.Where(s => s.Time.Weekday_flag).Sum(s => s.Total_Amount),
Weekends = g.Where(s => !s.Time.Weekday_flag).Sum(s => s.Total_Amount)

or:

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