作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的基础存储库类中
我编写了这个函数,以便可以从数据库中检索已排序的数据集合。
T 是在类级别定义的泛型
public abstract class RepositoryBase<T>
where T : class
public IList<T> GetAll<TKey>(Expression<Func<T, bool>> whereCondition, Expression<Func<T, TKey>> sortCondition, bool sortDesc = false)
{
if (sortDesc)
return this.ObjectSet.Where(whereCondition).OrderByDescending(sortCondition).ToList<T>();
return this.ObjectSet.Where(whereCondition).OrderBy(sortCondition).ToList<T>() ;
}
repo.GetAll (model=>model.field>0, model=>model.sortableField, true)
repo.GetAll<Model> (model=>model.field>0, model=>model.sortableField, true)
最佳答案
您正在使用 ObjectSet
实现IQueryable<T>
.这通过 System.Linq.Queryable
上的方法进行了扩展, 接受 Expression<Func<
参数。使用那些 Expression
是正确的参数,因为您打算在数据库中执行,而不是在本地执行。
repo.GetAll<Model> (model=>model.field>0, model=>model.sortableField, true)
Repository<Person> myRepo = new Repository<Person>();
myRepo.GetAll<DateTime>(p => p.Friends.Count() > 3, p => p.DateOfBirth, true);
关于linq - 时间:2019-01-01 标签:c#genericorderby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15064581/
在我的基础存储库类中 我编写了这个函数,以便可以从数据库中检索已排序的数据集合。 T 是在类级别定义的泛型 public abstract class RepositoryBase w
我是一名优秀的程序员,十分优秀!