IQueryable .Where to filter by properties on child objects

P

powpowqueen

Hi I have just started using IQueryable and have a question that is a
little more complicated than any examples i can find

Can anyone answer my problem?

I have a 2 classess

Product and Colour

A product has a stock count.
a product has one colour
a colour has many products.

I have a IQueryable<Colour> object and would like to write the Where
criteria on this to get
All colours that have a product with stock >= 50.

Any ideas?

I know that if i was doing this in reverse using IQueryable<Product> i
would write
..Where(e => e.Stock >= stock)
and then get the colour property.

But i really want to be able to do this from the iqueryable collection
of Colours

Thanks in advance
 
P

Pavel Minaev

Hi I have just started using IQueryable and have a question that is a
little more complicated than any examples i can find

Can anyone answer my problem?

I have a 2 classess

Product and Colour

A product has a stock count.
a product has one colour
a colour has many products.

I have a IQueryable<Colour> object and would like to write the Where
criteria on this to get
All colours that have a product with stock >= 50.

Any ideas?

I know that if i was doing this in reverse using IQueryable<Product> i
would write
.Where(e => e.Stock >= stock)
and then get the colour property.

But i really want to be able to do this from the iqueryable collection
of Colours

Keep in mind that all those LINQ queries (and lambdas in general) can
be nested. So:

colors.Where(c => c.Products.Any(p => p.Stock >= 50));
 
P

powpowqueen

Keep in mind that all those LINQ queries (and lambdas in general) can
be nested. So:

  colors.Where(c => c.Products.Any(p => p.Stock >= 50));- Hide quoted text -

- Show quoted text -

Thanks very much, this is exactly what i needed, - i think i was
almost there, i had
colours.where(c => c.Products.Where(p => p.Stock >= 50));

I needed to use Any :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top