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