简体   繁体   English

EntityFramework Core 6.0 Linq 组加入问题(关于“入”字)

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

I am using EF Core 6.0 Framework.. And I am coding Web API.. I want to join City and Town Entities like below codes.我正在使用 EF Core 6.0 框架。我正在编码 Web API ..我想加入城市和城镇实体,如下面的代码。
But the program is crashing if I use " into " word in LinQ query like this " into tmpTownArr " (Its giving GroupJoin Error, I wrote it under the picture)但是,如果我在 LinQ 查询中使用“ into ”字样“ into tmpTownArr ”,程序就会崩溃(它给 GroupJoin 错误,我写在图片下面)

On Below Picture No 1 and 3 not working, But No 2 and 4 working在下面的图片 1 和 3 不工作,但 2 和 4 工作

在此处输入图像描述

The Error Code On No 1: 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.

Why not redefine how you are using EF and use navigation properties instead of manual joins?为什么不重新定义如何使用 EF 并使用导航属性而不是手动连接?

Your problem can be simplified to您的问题可以简化为

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

The short answer is that you've written Linq that can't be translated to SQL.简短的回答是您编写的 Linq 无法转换为 SQL。

One option to fix this is to use AsEnumerable(), but be careful with this as it has a lot of potentially unwanted side effects.解决此问题的一种方法是使用 AsEnumerable(),但请注意这一点,因为它有很多潜在的不良副作用。

Otherwise you'll need to come up with an alternate approach.否则,您将需要提出另一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM