Entity Framework Core 1.0.0: Eager loading with Where-clause

c# entity-framework-core npgsql

Question

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

1
3
8/17/2016 7:02:56 AM

Popular Answer

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();
2
7/5/2016 11:53:13 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow