2

The examples with predicatebuilder show how you can define general logic using predicates. But I'm struggling with the following :

Suppose Product has a foreign key relationship with Category and Category has a DateTime? EndDate.

I can write some general logic to find active categories :

public partial class Category
{
   public static Expression<Funct<Category,bool>> IsActive()
   {
      return c=>c.EndDate.HasValue ? c.EndDate>DateTime.Now : true;
   }
}

so I can write

Categories.Where(Category.IsActive())

But I can't seem to make it work when querying Products, I would like to be able to write something like:

Products.Where(p=>p.Category.IsActive() && p.Name.Contains("beer"));

I could write another method

public static Expresision<Func<Product,bool>> IsCategoryActive()
{
   return p=>p.Category.EndDate.HasValue ? p.Category.EndDate>DateTime.Now ? true;
}

but I want to avoid defining this logic over and over again... I would be much nicer if I could define this once on Product

rekna
  • 5,313
  • 7
  • 45
  • 54
  • As for the "contains", see http://stackoverflow.com/questions/2369022/using-contains-in-linq-to-sql. – Gert Arnold Nov 26 '11 at 11:49
  • the contains is not the problem... it was just an example to show that I need to be able to combine IsActive with other criteria. – rekna Nov 26 '11 at 13:32

0 Answers0