英文:
EF Core: Unable to translate set operation after client projection has been applied
问题
我目前正在使用Entity Framework Core开发一个项目,遇到了LINQ查询的问题。在EF的旧版本(EF-6)中,这个查询运行良好,但在升级到最新版本(EF Core-7)后,我遇到了以下异常:
> “在应用客户端投影后无法翻译集操作。考虑将集操作移至最后的 'Select' 调用之前。”
以下是有问题的代码:
var qTypes = DbRead.datas.AsNoTracking().Where(x => SelectionTypes.Contains(x.type));
var q1 = qTypes
.GroupBy(x => x.type)
.Select(g => g.Where(x => x.timestamp <= fromTimestamp).OrderByDescending(x => x.timestamp).FirstOrDefault());
var q2 = qTypes.Where(x => x.timestamp > fromTimestamp && x.timestamp <= toTimestamp);
var qResult = q1.Union(q2).OrderBy(x => x.timestamp).ThenBy(x => x.type);
var entries = new List<SelectionEntry>();
foreach (var entity in qResult)
{
if (entity is null)
{
continue; // 跳过 - 由于q1中的FirstOrDefault引起
}
entries.Add(FromSelectionEntity(entity));
}
return entries;
我尝试了不同的方法来重构查询,但没有成功。对于如何解决这个问题的任何建议或见解将不胜感激。
在此先感谢您的帮助!
英文:
I'm currently working on a project using Entity Framework Core and I've come across an issue with a LINQ query. The query worked fine in previous versions of EF
(EF -6) but after upgrading to the latest version (EF core -7), I'm getting the following exception:
> "Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select' call."
Here is the problematic code
var qTypes = DbRead.datas.AsNoTracking().Where(x => SelectionTypes.Contains(x.type));
var q1 = qTypes
.GroupBy(x => x.type)
.Select(g => g.Where(x => x.timestamp <= fromTimestamp).OrderByDescending(x => x.timestamp).FirstOrDefault());
var q2 = qTypes.Where(x => x.timestamp > fromTimestamp && x.timestamp <= toTimestamp);
var qResult = q1.Union(q2).OrderBy(x => x.timestamp).ThenBy(x => x.type);
var entries = new List<SelectionEntry>();
foreach (var entity in qResult)
{
if (entity is null)
{
continue; // skip - caused by the FirstOrDefault in q1 above
}
entries.Add(FromSelectionEntity(entity));
}
return entries;
I have tried different approaches to restructure the query but without success. Any suggestions or insights on how to resolve this issue would be greatly appreciated.
Thank you in advance for your help!
答案1
得分: 1
尝试以下解决方法:
var qTypes = DbRead.datas.AsNoTracking().Where(x => SelectionTypes.Contains(x.type));
var q1Filtered = qTypes.Where(x => x.timestamp <= fromTimestamp);
var q1 =
from d in q1Filtered.Select(d => new { d.Type }).Distinct()
from q in q1Filtered
.Where(q => q.Type == d.Type)
.OrderByDescending(x => x.timestamp)
.Take(1)
select q;
var q2 = qTypes.Where(x => x.timestamp > fromTimestamp && x.timestamp <= toTimestamp);
var qResult = q1.Union(q2).OrderBy(x => x.timestamp).ThenBy(x => x.type);
var entries = qResult.Select(entity => FromSelectionEntity(entity)).ToList();
return entries;
注意:我已经将HTML转义字符(如>
和<
)替换为正常的代码比较运算符。
英文:
Try the following workaround:
var qTypes = DbRead.datas.AsNoTracking().Where(x => SelectionTypes.Contains(x.type));
var q1Filtered = qTypes.Where(x => x.timestamp <= fromTimestamp);
var q1 =
from d in q1Filtered.Select(d => new { d.Type }).Distinct()
from q in q1Filtered
.Where(q => q.Type == d.Type)
.OrderByDescending(x => x.timestamp)
.Take(1)
select q;
var q2 = qTypes.Where(x => x.timestamp > fromTimestamp && x.timestamp <= toTimestamp);
var qResult = q1.Union(q2).OrderBy(x => x.timestamp).ThenBy(x => x.type);
var entries = qResult.Select(entity => FromSelectionEntity(entity)).ToList();
return entries;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论