score:2

Accepted answer

Is this linq-to-entities? Then it is not possible to do any kind of filtering to the included entities that are eager loaded. Ladislav Mrnka wrote about it in his latest blog post.

It is of course possible to use linq-to-objects to handle the entities once loaded into memory, but if you only want a fraction of the available entities that would be inefficient.

A linq-to-objects solution:

var userGroups = this.ObjectContext.UserGroups.Include("Users")
.Select(ug => new UserGroup {
Name = ug.Name,
// Copy all fields
Users = ug.Users.Where(u => u.Active)
});

score:1

Could you reverse the statement to be

var userGroups = this.ObjectContext.Users.Include("UserGroups").Where(f=>f.IsActive == true);  

And then use a GroupBy on the UserGroups field?


Related Articles