predicatebuilder 的示例展示了如何使用谓词定义一般逻辑。但我正在努力解决以下问题:
假设 Product 与 Category 有外键关系,Category 有 DateTime?结束日期。
我可以编写一些通用逻辑来查找活动类别:
public partial class Category
{
public static Expression<Funct<Category,bool>> IsActive()
{
return c=>c.EndDate.HasValue ? c.EndDate>DateTime.Now : true;
}
}
所以我可以写
Categories.Where(Category.IsActive())
但是在查询产品时我似乎无法使其工作,我希望能够编写如下内容:
Products.Where(p=>p.Category.IsActive() && p.Name.Contains("beer"));
我可以写另一种方法
public static Expresision<Func<Product,bool>> IsCategoryActive()
{
return p=>p.Category.EndDate.HasValue ? p.Category.EndDate>DateTime.Now ? true;
}
但我想避免一遍又一遍地定义这个逻辑......如果我能在 Product 上定义一次,我会更好