C# 列表顺序

标签 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);
            }
        }

最佳答案

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

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

关于C# 列表顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484543/

相关文章:

c# - 在 Page_load 方法中只传递一次

c# - 将 UserControl ListBox ItemSsource 绑定(bind)到父 DataContext 时出错

asp.net - 运行测试时出错: "You must add a reference to assembly..."

c# - 是否可以使用 MVC/Razor 写出生成的有序数字序列,跨越内容页和母版页?

c# - 使用 ASP.NET MVC3 处理注册向导的正确方法是什么?

c# - JetBrains Rider - 配置应用程序设置覆盖

c# - 如何通过 Json 在 C# 上使用 telegram API?

c# - 在 Visual Studio 2010 中调试时出现 fatal error HRESULT=0x80131c08

asp.net - 将 MVC 添加到 ASP.NET 网站

javascript - 从列表中删除项目,knockout.js