简体   繁体   中英

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.
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)

On Below Picture No 1 and 3 not working, But No 2 and 4 working

在此处输入图像描述

The Error Code On No 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?

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.

One option to fix this is to use AsEnumerable(), but be careful with this as it has a lot of potentially unwanted side effects.

Otherwise you'll need to come up with an alternate approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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