【问题标题】:How to sort (order by) list of objects according to specific column如何根据特定列对对象列表进行排序(排序)
【发布时间】:2013-01-15 14:25:52
【问题描述】:

如何根据特定字段(sort expression)和方向(sort direction)对对象列表进行排序..

例如:

TransactionList.ToList<UserTransactionDTO>()

【问题讨论】:

    标签: c# asp.net linq sorting gridview


    【解决方案1】:

    假设您的列表是一个自定义类型的列表,并且您希望按一个或多个属性排序:

    var trans = transactions.OrderBy(t => t.PropertyName)
                            .ThenBy(t => t.DifferentPropertyName)
    

    如果您想改为降序排列:

    var trans = transactions.OrderByDescending(t => t.PropertyName)
                            .ThenByDescending(t => t.DifferentPropertyName)
    

    Enumerable.OrderBy Method

    【讨论】:

    【解决方案2】:
    public class Person
    {
        public String Name { get; set; }
        public int Age { get; set; }
    }
    
    var people = new List<Person>(); //Fill it
    var sorted = people.OrderBy(p => p.Name).ThenBy(p => p.Age);
    

    否则使用 OrderByDescending();

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      如果你想使用复杂的排序,你也可以使用IComparer&lt;T&gt;

      见:How sort a System.Collections.Generic.List in VB.Net?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-26
        • 2023-04-01
        • 2020-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-03
        相关资源
        最近更新 更多