views:

30

answers:

1

I've got an EDM with two tables Product and ProductCategory with a many-to-many relationship between both.

What I'm currently trying to do is to build a dynamic query to select the products related to the categories the user has selected through the UI.

In short I should build a query like the following but based one or more categories that I don't know at compile time.

var productQuery = context.Product.Where
            (p => p.ProductCategories.Any(c => c.CategoryId == id1 ||
                                               c.CategoryId == id2 || ...));

I've read a lot of things and I'm actually very new to linq so I really don't know where to start.

What is the best approach to do such queries ?

Thank you for your time.

A: 
var ids = new [] { id1, id2, // ... 
var productQuery = context.Product.Where(
                       p => p.ProductCategories.Any(c => ids.Contains(c.CategoryId)));
Craig Stuntz
As simple as that... well, I was looking way too far. Thank you.
Yves