簡體   English   中英

EntityFramework Core 6.0 Linq 組加入問題(關於“入”字)

[英]EntityFramework Core 6.0 Linq Group Join problem ( on "into" word)

我正在使用 EF Core 6.0 框架。我正在編碼 Web API ..我想加入城市和城鎮實體,如下面的代碼。
但是,如果我在 LinQ 查詢中使用“ into ”字樣“ into tmpTownArr ”,程序就會崩潰(它給 GroupJoin 錯誤,我寫在圖片下面)

在下面的圖片 1 和 3 不工作,但 2 和 4 工作

在此處輸入圖像描述

1號錯誤代碼:

The LINQ expression 'DbSet<City>()
.GroupJoin(
    inner: DbSet<Town>(), 
    outerKeySelector: c => c.Id, 
    innerKeySelector: t => t.CityId, 
    resultSelector: (c, tmpTownArr) => new { 
        c = c, 
        tmpTownArr = tmpTownArr
     })' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

為什么不重新定義如何使用 EF 並使用導航屬性而不是手動連接?

您的問題可以簡化為

var townCities = await context.Set<City>.Select(c => new { c, c.Towns }).ToListAsync();

簡短的回答是您編寫的 Linq 無法轉換為 SQL。

解決此問題的一種方法是使用 AsEnumerable(),但請注意這一點,因為它有很多潛在的不良副作用。

否則,您將需要提出另一種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM