我有一个 RepositoryBase 类,我在其中为我的实体框架上下文定义基本的 crud 方法。我有 All() 方法的这两个重载:
public virtual IQueryable<T> All<TKey>(Expression<Func<T, bool>> predicate)
{
return All().Where(predicate);
}
public virtual PagedResult<T> All<TKey>(int startRowIndex, int maximumRows,
Expression<Func<T, TKey>> orderingKey, Expression<Func<T, bool>> predicate,
bool sortDescending = false)
{
var subset = All().Where(predicate);
IEnumerable<T> result = sortDescending
? subset.OrderByDescending(orderingKey).Skip(startRowIndex).Take(maximumRows)
: subset.OrderBy(orderingKey).Skip(startRowIndex).Take(maximumRows);
//More code ommited
}
第一种方法总是需要我明确指定实体类型,但第二种方法不需要。为什么是这样?
例如,这不会编译:
return All(s => s.LoanApplicationId == loanApplicationId)
相反,我必须这样称呼它:
return All<LoanApplication>(s => s.LoanApplicationId == loanApplicationId)
但这确实编译:
return All(0,10, s => s.Name, s => s.LoanApplicationId == loanApplicationId, false)