我正在將我的 ASP.NET Core Web API 專案從 .NET Core 2.1 升級到 3.1。我還需要將 EF Core 從 2.1 升級到 3.1。
升級 EF 后,在 .NET Core 2.1 中作業的 Linq 查詢無法正常作業(可能是新版本不支持向后兼容或使用 Linq 發生變化)。
重寫 linq 查詢以使其正常運行的標準方法是什么?linq 中的哪些內容從 2.1 更改為 3.1?該問題與具有與 ANY() 等相關的操作的 select 子句有關。
var classes =
(
from z in Context.aaa.Where(x => x.Id == Id)
join b in Context.bbb on z.Id equals b.Id
join bc in Context.ccc.Where(x => x.IsActive == true) on b.Id equals bc.Id
join fcc in Context.ddd.Where(x => x.IsActive == true) on bc.Id equals fcc.Id
join fc in Context.eee.Where(x => x.IsActive == true )
.Include(i => i.Level) on fcc.Id equals fc.Id
select new
{
bc.IsUserRequired,
IsHiddenByOverrideOrExclusion = zoneList.Any(x => x.Id == fc.Id && x.IsHidden == true) || (!zoneList.Any(x => x.Id == fc.Id && x.IsHidden == false) && functioncheckFlag(fc, Id, category)),
}
).Distinct().AsEnumerable();
uj5u.com熱心網友回復:
EF Core 3.x 改變了翻譯行為。EF Core 不再在客戶端靜默處理資料。因此翻譯將失敗,因為functioncheckFlag無法轉換為 SQL,并且可能zoneList.Any也是zoneList本地集合。
要解決問題,你必須Distinct在客戶端做。
var classes =
(
from z in Context.aaa.Where(x => x.Id == Id)
join b in Context.bbb on z.Id equals b.Id
join bc in Context.ccc.Where(x => x.IsActive == true) on b.Id equals bc.Id
join fcc in Context.ddd.Where(x => x.IsActive == true) on bc.Id equals fcc.Id
join fc in Context.eee.Where(x => x.IsActive == true ) on fcc.Id equals fc.Id
select new
{
bc.IsUserRequired,
IsHiddenByOverrideOrExclusion = zoneList.Any(x => x.Id == fc.Id && x.IsHidden == true) || (!zoneList.Any(x => x.Id == fc.Id && x.IsHidden == false) && functioncheckFlag(fc, Id, category)),
}
)
.AsEnumerable()
.Distinct();
也已洗掉,如果您在查詢末尾Include有,則不需要。Select
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422280.html
標籤:
下一篇:LINQ-嵌套WHEREIN查詢
