我有一个方法,给出参数"bool sortAscending".现在我想使用LINQ根据此参数创建排序列表.我得到了这个:
var ascendingQuery = from data in dataList
orderby data.Property ascending
select data;
var descendingQuery = from data in dataList
orderby data.Property descending
select data;
Run Code Online (Sandbox Code Playgroud)
如您所见,两个查询仅在"升序"中有所不同."降".我想合并两个查询,但我不知道如何.有人有答案吗?
Jon*_*eet 113
您可以在IEnumerable或IQueryable上轻松创建自己的扩展方法:
public static IOrderedEnumerable<TSource> OrderByWithDirection<TSource,TKey>
(this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
bool descending)
{
return descending ? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
public static IOrderedQueryable<TSource> OrderByWithDirection<TSource,TKey>
(this IQueryable<TSource> source,
Expression<Func<TSource, TKey>> keySelector,
bool descending)
{
return descending ? source.OrderByDescending(keySelector)
: source.OrderBy(keySelector);
}
Run Code Online (Sandbox Code Playgroud)
是的,你在这里失去了使用查询表达式的能力 - 但坦率地说,在这种情况下,我认为你实际上并没有从查询表达式中受益.查询表达式非常适合复杂的事情,但是如果你只进行一次操作,那么只需要放一个操作就更简单了:
var query = dataList.OrderByWithDirection(x => x.Property, direction);
Run Code Online (Sandbox Code Playgroud)
Mar*_*ell 41
就如何实现而言,这会改变方法 - 从OrderBy/ThenBy到OrderByDescending/ThenByDescending.但是,您可以将排序单独应用于主查询...
var qry = from .... // or just dataList.AsEnumerable()/AsQueryable()
if(sortAscending) {
qry = qry.OrderBy(x=>x.Property);
} else {
qry = qry.OrderByDescending(x=>x.Property);
}
Run Code Online (Sandbox Code Playgroud)
有用吗?您可以动态创建整个"订单",但它更复杂...
另一个技巧(主要适用于LINQ-to-Objects)是使用-1/1的乘数.这仅对数字数据非常有用,但却是实现相同结果的一种厚颜无耻的方式.
如何通过所需的财产订购desc,
blah = blah.OrderByDescending(x => x.Property);
Run Code Online (Sandbox Code Playgroud)
然后做类似的事情
if (!descending)
{
blah = blah.Reverse()
}
else
{
// Already sorted desc ;)
}
Run Code Online (Sandbox Code Playgroud)
反向()太慢了吗?
除了@Jon Skeet给出的漂亮解决方案之外,我还需要ThenBy和ThenByDescending,所以我根据他的解决方案添加它:
public static IOrderedEnumerable<TSource> ThenByWithDirection<TSource, TKey>(
this IOrderedEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
bool descending)
{
return descending ?
source.ThenByDescending(keySelector) :
source.ThenBy(keySelector);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
141499 次 |
最近记录: |