内容简介:翻译自:https://stackoverflow.com/questions/11781990/how-do-i-specify-linqs-orderby-direction-as-a-boolean
我有一个具有此签名的简单数据类:
internal interface IMyClass {
string Letter { get; }
int Number { get; }
}
我希望能够根据字段(指定为字符串sortField)和方向(指定为bool isAscending)对此数据进行排序
目前我正在使用一个开关(每种情况下的升序逻辑就像)
IEnumerable<IMyClass> lst = new IMyClass[];//provided as paramater
switch (sortField)
{
case "letter":
if( isAscending ) {
lst = lst.OrderBy( s => s.Letter );
} else {
lst = lst.OrderByDescending( s => s.Letter );
}
break;
case "number":
if( isAscending ) {
lst = lst.OrderBy( s => s.Number );
} else {
lst = lst.OrderByDescending( s => s.Number );
}
break;
}
对于2个属性,这非常难看,但是当 排序 逻辑不同时,它就成了一个问题(我们也看到s => s.Number在代码中重复两次)
题
传递布尔值以选择排序方向的最佳方法是什么?
我试过的
我已经拆开了System.Core.dll并找到了OrderBy Extension方法实现:
排序依据:
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
){
return new OrderedEnumerable<TSource, TKey>(
source,
keySelector,
null,
false
);
}
OrderByDescending:
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
){
return new OrderedEnumerable<TSource, TKey>(
source,
keySelector,
null,
true
);
}
似乎有两个命名方法的目的是抽象这个布尔值.我无法轻松创建自己的扩展,因为OrderedEnumberable是System.Core的内部,并且写一个层来自bool – > methodName – >布尔似乎对我不对.
我想说你自己的扩展方法:
public static IEnumerable<T> Order<T, TKey>(this IEnumerable<T> source, Func<T, TKey> selector, bool ascending)
{
if (ascending)
{
return source.OrderBy(selector);
}
else
{
return source.OrderByDescending(selector);
}
}
然后你可以写:
lst = lst.Order( s => s.Letter, isAscending );
至于指定方法名称:我希望这不作为一个cop-out答案,但我认为你应该坚持使用选择器函数而不是传入一个字符串.走字符串路线并没有真正省去任何打字或提高清晰度(“字母”真的比s => s.Letter更快或更清晰)并且只会使你的代码变得更胖(你需要维护一些从字符串到选择器函数的映射,或编写自定义解析逻辑以在它们之间进行转换)以及可能更脆弱(如果你采用后一种方法,则存在相当高的错误概率).
如果您的目的是从用户输入中取一个字符串来自定义排序,当然,您别无选择,所以请随意忽略我令人沮丧的言论!
编辑:由于您接受用户输入,这就是我的意思是映射:
class CustomSorter
{
static Dictionary<string, Func<IMyClass, object>> Selectors;
static CustomSorter()
{
Selectors = new Dictionary<string, Func<IMyClass, object>>
{
{ "letter", new Func<IMyClass, object>(x => x.Letter) },
{ "number", new Func<IMyClass, object>(x => x.Number) }
};
}
public void Sort(IEnumerable<IMyClass> list, string sortField, bool isAscending)
{
Func<IMyClass, object> selector;
if (!Selectors.TryGetValue(sortField, out selector))
{
throw new ArgumentException(string.Format("'{0}' is not a valid sort field.", sortField));
}
// Using extension method defined above.
return list.Order(selector, isAscending);
}
}
上面显然不像从字符串动态生成表达式并调用它们那样聪明;根据您的偏好以及您所参与的团队和文化,这可以被视为优势或劣势.在这种特殊情况下,我认为我会投票支持手动绘图,因为动态表达路线感觉过度设计.
翻译自:https://stackoverflow.com/questions/11781990/how-do-i-specify-linqs-orderby-direction-as-a-boolean
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Go 语言的布尔值
- Haskell:非严格的布尔运算
- 6. Go 语言数据类型:字典与布尔类型
- golang leetcode 1106 解析布尔表达式
- 6. Go语言中的字典与布尔类型
- final修饰的Boolean(布尔值)可以被修改值?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Approximation Algorithms
Vijay V. Vazirani / Springer / 2001-07-02 / USD 54.95
'This book covers the dominant theoretical approaches to the approximate solution of hard combinatorial optimization and enumeration problems. It contains elegant combinatorial theory, useful and inte......一起来看看 《Approximation Algorithms》 这本书的介绍吧!