[英]Is it possible to pass an OrderBy expression as an argument?
我正在使用NHibernate构建一个存储库框架。 我的部分要求包括限制返回的结果数量或强制分页。 为了让NHibernate成功地从IQueryable表达式解析查询,它必须在任何.Skip()。Take()操作之前执行OrderBy或OrderByDescending操作。
这就是我当前签名的调用方式。
var actualExerciseJournals = repo.GetAll( pageIndex: pageNum++, recordsPerPage: 250, orderByFirst: exerciseJournal => exerciseJournal.JOURNALDATE, orderBySecond: exerciseJournal => exerciseJournal.MEMBERID);
这是界面签名。
IEnumerable<TEntity> GetAll(int pageIndex, int recordsPerPage, Expression<Func<TEntity, object>> orderByFirst, Expression<Func<TEntity, object>> orderBySecond, Expression<Func<TEntity, object>> orderByThird, Expression<Func<TEntity, object>> orderByFourth, params Expression<Func<TEntity, object>>[] include);
(include参数与问题无关)
是否有可能接受这样的参数作为参数的签名?
var actualExerciseJournals = repo.GetAll(pageIndex: 0, recordsPerPage: 250, collectionToOrder => collectionToOrder.OrderBy(x => x.JOURNALDATE).ThenBy(y => y.MEMBERID));
然后我就可以在我的存储库中应用这个表达式了,我不再对orderbys的数量有任何限制,或者它是orderbydecending还是不是。
谢谢
lambda表达式只是一个函数。 所以你应该能够使用Func<IEnumerable<TEntity>,IEnumerable<TEntity>>
。 你有效地希望传入一个接收IEnumerable
的函数并发出一个不同的IEnumerable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.