Home > Mobile >  How can I LINQ select from two EF DbSets and group join - but using AutoMapper
How can I LINQ select from two EF DbSets and group join - but using AutoMapper

Time:07-29

Note: These classes are related, but not part of the same Aggregate (like PurchaseOrder and OrderLine) - so I do not have a navigation property from "One" to "Many".

=== Entities ===

public class One
{
  public Guid Id { get; set; }
  public string Name { get; set; }
}
 
public class Many
{
  public Guid Id { get; set; }
  public string Name { get; set; }
  public Guid One { get; set; }
}

=== Contracts ===

public class OneWithMany
{
  public Guid Id { get; set; }
  public string Name { get; set; }
  public IEnumerable<Many>? ManyRelatedObjects { get; set; }
}

I want to select all One objects and any related Many objects from DbSet/DbSet into OneWithMany.

To ensure I don't miss properties added in future I am using ProjectTo in AutoMapper - but I can't work out how to fit it into the equation.

CodePudding user response:

Unfortunately, it seems Entity Framework does not support GroupJoin.

The solution is to do the projection and as much filtering as possible as two separate queries, and then combine them into a result in memory.

If you find EF related answers on the web related to GroupJoin make sure you check the example code to see if they are actually showing code working on arrays instead of DbSet.

  • Related