Home > Blockchain >  Filter entities with a field from another entity in linq query c#
Filter entities with a field from another entity in linq query c#

Time:09-19

I wanna join some tables in linq query. The problem is that I can not filter one of entities with field of another entity. In the below code b.createDate is not defiend, how can I do this query in linq?

From a in context.A
Join b in context.B
On a.Id equals b.AId
Join c in context.C.where(x => 
x.createDate >= b.createDate)
On b.Id equals c.BId into g
From result in 
g.DefaultIfEmpty()
Select result

CodePudding user response:

You have to use from instead of join in this case. As documented: Collection selector references outer in a non-where case

var query =
    from a in context.A
    join b in context.B on a.Id equals b.AId
    from c in context.C
      .Where(x => x.BId == b.Id && x.createDate >= b.createDate)
      .DefaultIfEmpty()
    select c;
  • Related