作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个函数返回 IQueryable
:
private IQueryable<string> GetActiveCellPhoneNumbersUpToDate(long serviceToken, DateTime date, bool? isPrepaid = null)
{
var to = date.AddDays(1).Date;
var query = ViewRepository
.All
.Where(i => i.ServiceToken == serviceToken)
.Where(i => i.Date < to);
if (isPrepaid.HasValue)
{
query = query.Where(i => i.IsPrepaid == isPrepaid);
}
query = query.OrderByDescending(i => i.Date);
var result = query
.GroupBy(i => i.CellPhoneNumber)
.Where(i => i.First().ActionId == (int)SubscriptionAction.Subscription)
.SelectMany(i => i.ToList())
.Select(i => i.CellPhoneNumber)
.Distinct();
return result;
}
var prepaidsCount = GetActiveCellPhoneNumbersUpToDate(serviceToken, DateTime.Date, true);
var postPaidsCount = GetActiveCellPhoneNumbersUpToDate(serviceToken, DateTime.Date, false);
Processing of the LINQ expression 'i => i .ToList()' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF Core. See https://go.microsoft.com/fwlink/?linkid=2101433 for more detailed information.
AsEnumerable
或
ToList
前复合体
Where
子句来执行那部分 LINQ 并将数据带入 RAM,然后继续我的查询。
create table Subscriptions
(
Id,
CellPhoneNumber,
ServiceId,
Date,
ActionId
)
John,+1-541-754-3010,15,2019-10-13 12:10:06.153,1
John,+1-541-754-3010,15,2019-10-18 12:10:06.153,2
2910-11-03
报告我们有多少订阅者然后约翰的最后一个 Action 是离开服务,他不应该被计算在内。
最佳答案
依赖于间隔或记录的当前状态的查询可能很棘手。通常,我们必须搜索在指定时间段内具有一种状态但没有另一种状态的订阅。这至少需要一个子查询或 CTE。即使使用索引,这也可能很昂贵,因为它需要对目标表进行两次查找或扫描。
欢迎任何避免这种情况的技巧。在这种特殊情况下,操作 ID 为 1 和 2,获取活跃订阅者的一种简单方法是获取 MAX(ActionID) 不是 2 或小于 2 的订阅者,例如:
SELECT COUNT(Distinct cellnumber)
FROM Subscriptions
WHERE Date <=@reportDate ....
GROUP by CellNumber
HAVING MAX(ActionID)<2
var actives= ctx.Subscriptions
.Where(sub=>sub.Date <= reportDate )
.GroupBy(sub=>sub.CellNumber)
.Where(grp=>grp.Max(sub=>sub.ActionId)<2) // Results in a HAVING clause
.Distinct()
.Count();
var query = ctx.Subscriptions
.Where(sub=>sub.Date <= reportDate && sub.ServiceToken == serviceToken);
if(isPrepaid.HasValue)
{
query = query.Where(sub => sub.IsPrePaid==isPrepaid);
}
var actives= query.GroupBy(sub=>sub.CellNumber)
.Where(grp=>grp.Max(sub=>sub.ActionId)<2)
.Distinct()
.Count();
Subscriptions
放入一个时态表中,并简单地计算在某个时间点具有特定状态的订阅。我们可以使用:
SELECT COUNT(DISTINCT CellPhoneNumber)
FROM Subscriptions FOR SYSTEM_TIME AS OF @someTime
WHERE ActionID<2
var query = ctx.Subscriptions
.FromSqlRaw("select * from Subscriptions FOR SYSTEM_TIME AS OF {0}",
reportDate)
.Where(sub=>sub.Date <= reportDate && sub.ServiceToken == serviceToken);
if(isPrepaid.HasValue)
{
query = query.Where(sub => sub.IsPrePaid==isPrepaid);
}
var actives= query.Distinct()
.Count();
关于c# - 中断更改后如何在 EF Core 3.1 中创建复杂的动态查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59318465/
我是一名优秀的程序员,十分优秀!