我查看了EF通用存储库的许多示例。找不到OrderBy
和Include
的实例。我不知道如何在控制器(orderBy:?,includes:?)
中填写问题点我被困在这里。我在等待您的帮助
存储库
public List<T> Get(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
E_ComDBContext db = new E_ComDBContext();
DbSet<T> dbSet;
dbSet = db.Set<T>();
IQueryable<T> query = dbSet;
foreach (Expression<Func<T, object>> include in includes)
query = query.Include(include);
//if (select != null)
// query = query.Select(select);
if (filter != null)
query = query.Where(filter);
if (orderBy != null)
query = orderBy(query);
return query.ToList();
}
Contoller
public JsonResult List()
{
Repository<Product> _repository = new Repository<Product>();
var list = _repository.Get(x=>x.Price>10,orderBy:?,includes:?);
return Json(list, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:2)
我会尝试的。
var list = _repository.Get(filter: x => x.Price > 10, orderBy: x=> x.OrderBy(y => y.Price), x => x.Location, x=> x.Brand);