2

In basic I'm looking for the simplest way to get an associated collection of an entity in one request.

using (var context = new DbContext())
 {
   context.Users.Attach(user);
   context.Entry(user)
   .Collection(f => f.Followers)
   .Query()
   .Where(x => x.Whatever)
   //.Collection(r => r.Requests) doesn't work
   //.Collection(b => b.Blocks) doesn't work
   .Load();
 }

I'm aware that I could split these up into 3 different requests, but I'm wanting only one trip to the db.

Alternatively I was able to pull this off:

var user = context.Users
           .Include(f => f.Followers)
           .Include(r => r.Requests)

The problem with the approach above is I'm unable to filter the collections in the include with a .Where clause.

This question is very much: EF 6 filtering child collections

As well as: Entity Framework Query multiple collections

Neither of which were able to be pulled off with one request, so I'm guessing its not possible?

Community
  • 1
  • 1
Faraji Anderson
  • 653
  • 6
  • 18
  • If you want to filter the related entities in one round trip, then you will need to project your query with the expected result – ocuenca Jan 23 '17 at 19:22
  • 2
    Actually using a single SQL (db trip) to return multiple child collections like in the `Include` example is much more inefficient than using separate roundtrips because the returned result set must be a Cartesian product of all the child records with union-ed fields (most of them being `null`). So even if you can make it somehow working, don't do it that way. – Ivan Stoev Jan 23 '17 at 19:25
  • @octavioccl can you elaborate? – Faraji Anderson Jan 23 '17 at 19:25

1 Answers1

0

EF in its base does not support that.

You can split it into 3 separate queries and use some external library to do it in single round trip, for example Future Queries of EntityFramework.Extended nuget library.

Zapo
  • 211
  • 1
  • 6