使用 Entity Framework 4.0,我用生成的代码定义了两个简单的实体,如下所示:
public partial class Person : EntityObject
{
public string Name { ... generated code ... }
public DateTime Birthday { ... generated code ... }
}
public partial class Sale : EntityObject
{
public int ProductId { ... generated code ... }
public DateTime DateSold { ... generated code ... }
}
这个特殊的例子是人为的,但代表了我面临的一个真正的问题。通常在我的代码中,我想将实体限制为出现在特定日期范围内的实体。所以我有很多实例:
entities.Sales.Where(sale => sale.DateSold > startDate && sale.DateSold < endDate);
entities.People.Where(person => person.Birthday > startDate && person.Birthday < endDate);
是否可以设置一个可以处理此问题的通用方法? 就像是
entities.Sales.WithinRange(startDate, endDate);
将是完美的,我意识到我可以为 each 设置一个扩展方法IQueryable<T>
,但我希望能够灵活地在公共代码中使用 WithinRange() 方法。例如:
public static IQueryable<T> GetSortedNonNullObjectsInRange<T>(this IQueryable<T> data, DateTime startDate, DateTime endDate) where T : IHasDateDefined
{
return data.Where(entity => entity != null).WhereInRange(startDate, endDate).OrderBy(entity => entity.Date);
}
当我尝试使用通用接口(如 IHasDateDefined)时,接口上的 Date 属性会根据需要返回生日或 DateSold,但随后实体框架会抛出无法构建表达式的错误。