Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group join in EF Core 3.1

I am trying to group join in EF core 3.1 the problem it returns

Processing of the LINQ expression 'DbSet failed. This may indicate either a bug or a limitation in EF Core

my code is like this

 var employees = await (from enrollment in RepositoryContext.Enrollments
                join allowance in RepositoryContext.Allowances.Include(y=>y.AllowanceType) on enrollment.EmployeeId equals allowance.EmployeeId
                    into allowances

                select new
                {
                    enrollment,
                    allowances

                }
            ).AsNoTracking().ToListAsync();

the allowances is list of items , is there any workaround to run the query like this cause i need it for better berformance.

like image 757
Mustafa Mohamed Avatar asked Mar 08 '20 14:03

Mustafa Mohamed


People also ask

How do I join multiple tables in EF core?

The LINQ join operator allows us to join multiple tables on one or more columns (multiple columns). By default, they perform the inner join of the tables. We also learn how to perform left joins in EF Core by using the join operator & DefaultIfEmpty method. Also left join with where clause.

What is a group join?

The group join is useful for producing hierarchical data structures. It pairs each element from the first collection with a set of correlated elements from the second collection. For example, a class or a relational database table named Student might contain two fields: Id and Name .

How do I join the EF core?

While the LINQ Join has outer and inner key selectors, the database requires a single join condition. So EF Core generates a join condition by comparing the outer key selector to the inner key selector for equality.

Can Core 3.1 Use EF Core 5?

EF Core 5.0 requires a . NET Standard 2.1 platform. This means EF Core 5.0 will run on . NET Core 3.1 or .


1 Answers

Here Query with GroupBy or GroupJoin throws exception is the now closed GitHub issue/discussion where I was trying to convince EF Core team to add GroupJoin translation. They refused to do that and opened the useless Query: Support GroupJoin when it is final query operator #19930 where I continue the fight for such translation. So please go there and comment/vote up for the full translation request.

You will also find there the workaround - instead of unsupported GroupJoin use the equivalent supported correlated subquery approach, e.g. replace

join allowance in RepositoryContext.Allowances.Include(y => y.AllowanceType)
    on enrollment.EmployeeId equals allowance.EmployeeId
into allowances

with

let allowances = RepositoryContext.Allowances.Include(y => y.AllowanceType)
    .Where(allowance => enrollment.EmployeeId == allowance.EmployeeId)
like image 128
Ivan Stoev Avatar answered Nov 02 '22 10:11

Ivan Stoev