EF: Include with where clause

Several guys ask me if it’s possible to add a condition in the Include method and the answer is no.

However you can do an equivalent like this:

from cWithP in

    (from c in context.Categories

     select new

     {

         Category = c,

         Products = from p in c.Products

                    where p.UnitPrice > 20

                    select p

     }).AsEnumerable()

select cWithP.Category;

or the condensed equivalent:

context.Categories.Select(c => new { Category = c, Products = c.Products.Where(p => p.UnitPrice > 20) }).AsEnumerable().Select(cp => cp.Category);

Published Wed, Oct 7 2009 0:10 by Matthieu MEZIL

Leave a Comment

(required) 
(required) 
(optional)
(required)