通过订购多领域列表(C#)? [英] Order a List (C#) by many fields?
问题描述
我想很多领域,不只是一个命令在C#中的对象的列表。例如,假设我有一个称为X有两个属性,A和B类,我有以下对象,按照这个顺序:
I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order:
object1 => A = A,b =H结果
Object2的=> A =A,b =C结果
object3 => A =b,b =X 结果
object4 => A =b,b =b
object1 => A = "a", B = "h"
object2 => A = "a", B = "c"
object3 => A = "b", B = "x"
object4 => A = "b", B = "b"
和我想首先为了通过属性列表,当它们等于由b元素,因此为了将是:
and I want to order the list by A attribute first, and when they are equals, by B element, so the order would be:
一,C结果,
中的,h的< BR>
b,b结果
b,X
"a" "c"
"a" "h"
"b" "b"
"b" "x"
据我所知,排序依据法秩序通过一个参数
As far as I know, the OrderBy method order by one parameter.
问:我如何通过多个字段订购C#列表
Question: How can I order a C# List by more than one field?
非常感谢你。
推荐答案
使用 ThenBy
Customer.OrderBy(C => c.LastName).ThenBy(C => c.FirstName)
请参阅MSDN:的http:// msdn.microsoft.com/en-us/library/bb549422.aspx
这篇关于通过订购多领域列表(C#)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!