C#按顺序列出

C#按顺序列出,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我正在尝试为Razor Webgrid实现自定义排序。具体地说,我想对webgrid本身中没有出现的列进行排序 我要发疯了,试图弄明白为什么匹配列表不能排序。任何想法都将不胜感激 多谢各位 ResultsDisplayModel foo; // MVVM class to be bound to the view // List of Matching is bound to a web

我正在尝试为Razor Webgrid实现自定义排序。具体地说,我想对webgrid本身中没有出现的列进行排序

我要发疯了,试图弄明白为什么匹配列表不能排序。任何想法都将不胜感激

多谢各位

        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);
            }
        }
resultsDisplayModelFoo;//要绑定到视图的MVVM类
//匹配列表已绑定到webgrid
//匹配的公共列表列表{get;set;}
//TotalDebt是ResultsModel的一个基本类,称为费用
如果(排序=“债务负担”)
{
如果(foo.bSortDirection)
{
foo.bSortDirection=false;
foo.ListOfMatching.OrderByDescending(x=>x.TotalDebt);
}
其他的
{
foo.bSortDirection=true;
foo.ListOfMatching.OrderBy(x=>x.TotalDebt);
}
}

默认情况下,LINQ中的扩展方法没有副作用(也就是说,它们不会修改原始集合)。您必须将结果集合分配给新变量或覆盖旧变量

foo.ListOfMatchingColleges = foo.ListOfMatchingColleges
                                .OrderBy(x => x.TotalDebt)
                                .ToList();

OrderBy和OrderByDescending不会修改列表,但会返回已排序元素的新列表。您必须重新分配您的属性:

foo.ListOfMatchingColleges = foo.ListOfMatching.OrderByDescending(x => x.TotalDebt);