简体   繁体   English

向此 ef 核心查询添加连续计数

[英]Add consecutive count to this ef core query

I have the below query in Entity Framework Core that reports on amounts of sales, grouped by customer, between given dates.我在 Entity Framework Core 中有以下查询,它报告给定日期之间按客户分组的销售额。

IEnumerable<SalesDataModel> currentSales =
(from s in sales
where s.Date >= [StartDate] && u.Date <= [EndDate]
join c in customers on s.Acct equals c.Account
group s by new { s.Name, c.City } into g
select new SalesDataModel { 
    CustName = g.Key.Name,
    City = g.Key.City,
    MaxBorrowed = g.Max(x => x.Price),
    Average = g.Average(x => x.Price),
    NumDays = 
        g.Sum(x => x.Price > 0 ? 1 : 0),
    ConsecutiveDays = //???
}).ToList();

Basically I want to count the number of times within the date span that a given customer made a purchase two days or more in a row.基本上我想计算给定客户在连续两天或更长时间内购买的次数。 So, start with Day 1, and compare it to Day 2;因此,从第 1 天开始,然后将其与第 2 天进行比较; if Price > 0 for both days, iterate a counter by one.如果这两天的 Price > 0,则以 1 为单位迭代计数器。 Then check Day 2 against Day 3 with the same criteria, and repeat for as many records as I have for that customer.然后用相同的标准检查第 2 天和第 3 天,并重复我为该客户所拥有的尽可能多的记录。 The counter should not reset to zero if a zero or null Price value is encountered, but it should reset for the next customer.如果遇到零或 null 价格值,计数器不应重置为零,但它应该为下一个客户重置。

I found this SO Post asking something similar, but there's got to be an easier way.我发现这个 SO Post问了类似的问题,但必须有一种更简单的方法。 I'm using EF Core 5.我正在使用 EF Core 5。

Also, full disclosure, this post is a continuation from a previous post .另外,完全公开,这篇文章是上一篇文章的延续。 Since the answer to this question might not be as simple as I hope, a second post seemed the better way.由于这个问题的答案可能不像我希望的那么简单,所以第二篇文章似乎是更好的方法。

This query is going to require a FromSqlRaw execution of an SP to make this work.此查询将需要 SP 的 FromSqlRaw 执行才能使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM