Reputation: 4328
How would I pass one or more lambda order by clauses to a method and append it to a linq to entities query like below? I don't want to pass in strings, which I've seen discussed before, it should be lambda expressions for maintainability/refactoring/compile support.
public IList<UserRating> GetUserRatings(int userId, IPager pager, IOrderedQueryable<UserRating> orderBy)
{
return _userRatingRepository.Table.Where(a => a.UserId == userId).Skip(pager.Skip).Take(pager.PageSize). /* orderBy goes here */ .ToList();
}
Upvotes: 0
Views: 705
Reputation: 34269
Heres how I do it:
public IList<UserRating> GetUserRatings<TKey>(
int userId,
IPager pager,
Func<UserRating, TKey> orderBy)
{
return _userRatingRepository
.Table
.Where(a => a.UserId == userId)
.Skip(pager.Skip)
.Take(pager.PageSize)
.OrderBy(orderBy)
.ToList();
}
usage:
GetUserRatings(1,pager, u=> u.Id)
Upvotes: 1