Can I pass an OrderBy expression as an argument?

I am creating a repository structure using NHibernate. Part of my requirements include limiting the number of returned results or forcing paging. For NHibernate to successfully parse a query from an IQueryable expression, it must execute OrderBy or OrderByDescending before any .Skip () operation. Take ().

So my current signature is called.

var actualExerciseJournals = repo.GetAll( pageIndex: pageNum++, recordsPerPage: 250, orderByFirst: exerciseJournal => exerciseJournal.JOURNALDATE, orderBySecond: exerciseJournal => exerciseJournal.MEMBERID); 

This is the signature of the interface.

 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); 

(The include argument is not relevant)

Is it possible to have a signature that takes something like this as a parameter?

 var actualExerciseJournals = repo.GetAll(pageIndex: 0, recordsPerPage: 250, collectionToOrder => collectionToOrder.OrderBy(x => x.JOURNALDATE).ThenBy(y => y.MEMBERID)); 

Then I could apply this expression in my repository, and I would no longer have restrictions on the number of orderbys or its orderbydescending or not.

thanks

+4
source share
1 answer

A lambda expression is just a function. That way you can use Func<IEnumerable<TEntity>,IEnumerable<TEntity>> . You really want to pass a function that takes an IEnumerable and throws another IEnumerable .

+3
source

Source: https://habr.com/ru/post/1489345/


All Articles