Scott Baker
Scott Baker

Reputation: 10443

Is there a way to do this with a LINQ Query?

<< Obfuscated >>

In my repository I declare IQueryable<Foo> foos and ConfigEntities db = new ConfigEntities() I then query the db with the linq query

foos = from f in _db.Foos
       select f;

Each Foo has an individual collection of EntityCollection<Bar> bars that is automagically populated by the Entity Framework.

I want to iterate over the foos collection and over the bars collections in each Foo, and modify the bars collection based on the date, something like:

from foo in foos
    (from bar in foo.bars
         where bar.Date < someDate && bar.Date >= someOtherDate
         select bar)
     select foo

So I get back all of the foos with a subset of the original bars. I'm not sure how, but I think I want to do a select within a select or something of that sort.

Any help would be appreciated.

Upvotes: 0

Views: 99

Answers (1)

svick
svick

Reputation: 244777

from foo in foos
from bar in foo.bars
where bar.Date == DateTime.Now
select new { foo, bar }

or (edited, considering you don't have the corresponding constructor for Foo)

from foo in foos
select new
{
  Foo = foo, 
  Bars = from bar in foo.bars
         where bar.Date == DateTime.Now
         select bar
}

This will give a collection of “tuples”, where each will contain the original Foo (with all the Bars) and also a collection of the Bars you want.

Upvotes: 3

Related Questions