Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core 1.0.0: Eager loading with Where-clause

When I used EntityFramework 7.0.0-rc1 I wrote something like this:

context.MyEntity
.Include(e=>e.MySubEntities)
.Where(e=>e.MySubEntities.Contains(value))
.Where(e=>e.Status==1)
.ToList();

In EFCore 1.0.0 the same code throws an exception. I suppose the Include at the moment the first Where runs hasn't loaded the data. So now I need first instantiate my collection with all related data then query it. Previous approach seemed to be optimized since there was the only database query (wasn't it?). How can I do the same now?

P.S. I'm using Npgsql.EntityFrameworkCore.PostgreSQL 1.0.0

like image 294
Slip Avatar asked Mar 17 '26 01:03

Slip


1 Answers

You cannot use Contains in EF Core like that because the value cannot not be translated in the expression tree:

Just use it like that:

 var tmp = myConext.MyEntity
          .Include(e=>e.MySubEntities)
          .Where(x => x.MySubEntities.Select(id=>id.MySubEntitiesId).Contains(value.MySubEntitiesId))
          .Where(e=>e.Status==1)
          .ToList();
like image 170
Bassam Alugili Avatar answered Mar 19 '26 15:03

Bassam Alugili