开发者

Dynamic query with Linq To Entities

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 st开发者_StackOverflow社区art.

What is the best approach to do such queries ?

Thank you for your time.


var ids = new [] { id1, id2, // ... 
var productQuery = context.Product.Where(
                       p => p.ProductCategories.Any(c => ids.Contains(c.CategoryId)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜