C# 两个或多个属性的 IQueryable 顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9787254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IQueryable order by two or more properties
提问by Yannis
I am currently ordering a list of custom objects using the IQueryable OrderBy method as follows:
我目前正在使用 IQueryable OrderBy 方法订购自定义对象列表,如下所示:
mylist.AsQueryable().OrderBy("PropertyName");
Now I am looking to sort by more than one property. Is there any way to do that?
现在我希望按多个属性进行排序。有没有办法做到这一点?
Thanks, Yannis
谢谢,雅尼斯
采纳答案by Nikolay
OrderBy(i => i.PropertyName).ThenBy(i => i.AnotherProperty)
In OrderBy and ThenBy you have to provide keySelector function, which chooses key for sorting from object. So if you know property name only at runtime then you can make such function with Reflection like:
在 OrderBy 和 ThenBy 中,您必须提供 keySelector 函数,该函数从对象中选择要排序的键。因此,如果您仅在运行时知道属性名称,那么您可以使用 Reflection 制作这样的功能,例如:
var propertyInfo = i.GetType().GetProperty("PropertyName");
var sortedList = myList.OrderBy(i => propertyInfo.GetValue(i, null))
But it will be slower, then direct access to property. Also you can "compile" such function on the fly with Linq.Expressions and it will work faster than reflection but it is not very easy. Or you can use CollectionViewSource and their sorting ablilities in WPF.
但是会比较慢,然后直接访问属性。您也可以使用 Linq.Expressions 即时“编译”此类函数,它的运行速度比反射快,但不是很容易。或者您可以在 WPF 中使用 CollectionViewSource 及其排序能力。
And don't forget that OrderBy() returns sorted enumerable and it does not sort your existed List inplace. In your example you did not save sorted list to variable.
并且不要忘记 OrderBy() 返回已排序的可枚举并且它不会就地对您现有的 List 进行排序。在您的示例中,您没有将排序列表保存到变量。
回答by Darin Dimitrov
You could use .ThenBy:
你可以使用.ThenBy:
var result = mylist
.AsQueryable()
.OrderBy(x => x.PropertyName)
.ThenBy(x => x.SomeOtherProperty);
回答by Chris Gessler
You probably want to use the ThenBy extension method to be able to sort by multiple fields
您可能希望使用 ThenBy 扩展方法能够按多个字段排序
return myList.AsQueryable().OrderBy(m=>m.Property1).ThenBy(m => m.Property2);
If you want dynamic Linq, look at LinqKit. I recently implemented Microsoft's dynamic Linq library from hereand was able to sort by two fields using a string.
如果您想要动态 Linq,请查看LinqKit。我最近从这里实现了 Microsoft 的动态 Linq 库,并且能够使用字符串按两个字段进行排序。
Awesome stuff! Not sure if this will be in .NET 5 or not.
很棒的东西!不确定这是否会出现在 .NET 5 中。
回答by ZenoArrow
As others have suggested, you can use 'ThenBy'. If you want to convert a string to a different value before using it, this is possible too, for example...
正如其他人所建议的那样,您可以使用“ThenBy”。如果您想在使用之前将字符串转换为不同的值,这也是可能的,例如...
var sortedSystemTestResultsList = systemTestResultsList.OrderBy(s =>
{
DateTime dt;
if (!DateTime.TryParse(s.testPointCompletedDate, out dt)) return DateTime.MaxValue;
return dt;
}).ThenBy(s =>
{
Int32 tpID;
if (!Int32.TryParse(s.testRunResultID, out tpID)) return Int32.MaxValue;
return tpID;
});

