1

I found this http://msdn.microsoft.com/en-US/data/jj574232 however this seem to only work on a single entity

var blog = context.Blogs.Find(1); 

// Load the posts with the 'entity-framework' tag related to a given blog 
context.Entry(blog) 
    .Collection(b => b.Posts) 
    .Query() 
    .Where(p => p.Tags.Contains("entity-framework") 
    .Load();

Since blogs is not just one entity... but rather a collection in and of itself. Does anyone know how to rewrite this so that .Entry is operating over the collection of blogs. I can't find any documentation on if something like this is possible

This means you wouldn't have to grab one in particular and do operations on it but it would loop over each and perform the operation.

context.Blogs.ForEach(entity in Blogs).Collection(b => b.Posts) 
    .Query() 
    .Where(p => p.Tags.Contains("entity-framework") 
    .Load();
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
DRobertE
  • 3,478
  • 3
  • 26
  • 43
  • Since you already have blogs collection you can do something like – Girish Sakhare Sep 17 '14 at 06:33
  • possible duplicate of [EF: Include with where clause](http://stackoverflow.com/questions/16798796/ef-include-with-where-clause) – Yuliam Chandra Sep 17 '14 at 08:41
  • somewhat of a dup but that one uses anonymous projects to go one level deep. The above example would allow multiple levels but only seems to work against one top level entity at a time... I'm looking for a combination of the two so that the anonymous projects don't get ugly when going multi-level, multi-filter – DRobertE Sep 17 '14 at 14:55
  • you just need to change `b.Passengers.Where(p => p.Awake)` to `b.Posts.Where(p => p.Tags.Select(t => t.NameOrElse).Contains("entity-framework"))` – Yuliam Chandra Sep 17 '14 at 16:45
  • tried something similar *context.ProductSubCategories.FirstOrDefaultAsync(p => p.ProductItems.Where(pi => (pi.ProductLocations.AsQueryable().Where( pl=>pl.StartDate > date) ) ) ); But it's telling me Cannot convert expression type ProductLocations to return type bool on the where inside the productitems.where* – DRobertE Sep 17 '14 at 17:30
  • Keep in mind I want to get back the subcategory and all products and all locations... but filter out those locations where there start date falls outside of a range – DRobertE Sep 17 '14 at 17:33

1 Answers1

0

I think this should do the trick:

var posts = context.Blogs
                   .SelectMany(x => x.Posts)
                   .Where(x => x.Tags.Contains("entity-framework");
Teppic
  • 2,506
  • 20
  • 26
  • Problem with this is, it would return Post, where tags contains blah blah blah, and not blogs, with post where tags contains blah blah blah... need to filter out the lower level entities but still return all top level entities... that's the tricky part. wanted to do it without using all kinds of annonymous projections cause that gets ugly – DRobertE Sep 17 '14 at 14:06