C#List Order By

Sco*_*ter 2 c# asp.net-mvc-3

我正在尝试为Razor Webgrid实现自定义排序.具体来说,我想排序一个没有出现在webgrid本身的列.

我想出去解决为什么ListOfMatching不会排序的问题.任何想法将不胜感激.

谢谢

        ResultsDisplayModel foo;        // MVVM class to be bound to the view
                                        // List of Matching is bound to a webgrid

//    public List<ResultsModel> ListOfMatching { get; set; }
//    TotalDebt is in a base class of ResultsModel called Expenses

        if (sort == "DebtBurden")
        {

            if (foo.bSortDirection)
            {
                foo.bSortDirection = false;
                foo.ListOfMatching.OrderByDescending(x => x.TotalDebt);
            }
            else
            {
                foo.bSortDirection = true;
                foo.ListOfMatching.OrderBy(x => x.TotalDebt);
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ufu*_*arı 6

LINQ中的扩展方法默认是无副作用的(意思是,它们不会就地修改原始集合).您必须将生成的集合分配给新变量或覆盖旧变量.

foo.ListOfMatchingColleges = foo.ListOfMatchingColleges
                                .OrderBy(x => x.TotalDebt)
                                .ToList();
Run Code Online (Sandbox Code Playgroud)