![](/img/trans.png)
[英]OData $top query in LINQ expression could not be transalted and will be evaluated locally using EF Core and .NET Core 2.2
[英]EF Core expression being evaluated locally… why?
我在數據庫上下文中有一個看起來像這樣的方法:
public override async Task<IEnumerable<FeedMessage>> GetLatestFeeds(
int userId,
int groupId,
int maxResults = 15,
long lastId = 0)
{
if (lastId == 0) lastId = long.MaxValue;
var userSpecific =
FeedMessages.Where(fm =>
fm.UserId.HasValue && fm.UserId.Value == userId && fm.Id < lastId && !fm.IsDeleted);
var groupSpecific =
FeedMessages.Where(fm =>
fm.UserId == null && fm.GroupId.HasValue && fm.GroupId.Value == groupId && fm.Id < lastId && !fm.IsDeleted);
var siteWide =
FeedMessages.Where(fm =>
fm.UserId == null && fm.GroupId == null && fm.Id < lastId && !fm.IsDeleted);
var feeds = await
userSpecific.Union(groupSpecific).Union(siteWide)
.OrderByDescending(x => x.Id)
.Take(maxResults)
.ToListAsync();
return feeds.OrderBy(x => x.Id);
}
這里的想法是,我想獲取特定於用戶,特定於組或通用的記錄,按ID進行組織,然后返回前X個結果。
如果運行此命令,則會出現一整屏錯誤:
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Union({from FeedMessage fm in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MySolution.Common.Entities.FeedMessage]) where ((((([fm].UserId == null) AndAlso ([fm].GroupId != null)) AndAlso (Convert([fm].GroupId, Int32) == __groupId_2)) AndAlso ([fm].Id < __lastId_3)) AndAlso Not([fm].IsDeleted)) select [fm]})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Union({from FeedMessage fm in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MySolution.Common.Entities.FeedMessage]) where (((([fm].UserId == null) AndAlso ([fm].GroupId == null)) AndAlso ([fm].Id < __lastId_4)) AndAlso Not([fm].IsDeleted)) select [fm]})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Union({from FeedMessage fm in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MySolution.Common.Entities.FeedMessage]) where ((((([fm].UserId == null) AndAlso ([fm].GroupId != null)) AndAlso (Convert([fm].GroupId, Int32) == __groupId_2)) AndAlso ([fm].Id < __lastId_3)) AndAlso Not([fm].IsDeleted)) select [fm]})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Union({from FeedMessage fm in value(Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MySolution.Common.Entities.FeedMessage]) where (((([fm].UserId == null) AndAlso ([fm].GroupId == null)) AndAlso ([fm].Id < __lastId_4)) AndAlso Not([fm].IsDeleted)) select [fm]})' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'orderby [x].Id desc' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Take(__p_5)' could not be translated and will be evaluated locally.
這里發生了什么? 我該如何解決? 這將是一張大表,在本地進行評估會破壞系統。
我最終在EF Core的github網站上發布了一個錯誤報告 ,幾個小時后,我得到了可以使用的響應。 事實證明,服務器端操作尚不支持Union()
, Concat()
, Except()
和Intersect()
。 :\\
在我的網絡搜索中,這並不是立即可見的。
根據開發團隊的建議,我可以這樣重寫查詢:
return FeedMessages
.Where(fm => fm.Id < lastId && !fm.IsDeleted && (
(fm.UserId.HasValue && fm.UserId.Value == userId) ||
(fm.UserId == null && fm.GroupId.HasValue && fm.GroupId.Value == groupId) ||
(fm.UserId == null && fm.GroupId == null)))
.OrderByDescending(x => x.Id)
.Take(maxResults)
.OrderBy(x => x.Id)
.ToListAsync();
...這使我脫離了危機模式,但我想拋給所有EF Core開發人員,看看這些帖子,這是重要的功能,應盡快在現實中優先考慮(IMO)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.