Jen

LINQ自訂Sort子句

private IQueryable<Customer> AddQuerySorting(IQueryable<Customer> query, Sorter sorter)
{
	if (String.IsNullOrEmpty(sorter.SortField))
		return query;

	var param = Expression.Parameter(typeof(Customer), "customer");
	var sortExpression = Expression.Lambda<Func<Customer, object>>
	(Expression.Convert(Expression.Property(param,
                  sorter.SortField), typeof(object)), param);

	if (sorter.SortDirection == SortDirection.Asc)
		query = query.OrderBy(sortExpression);
	else
		query = query.OrderByDescending(sortExpression);

	return query;
}
 
 
參考

0 意見: