簡體   English   中英

C#List Order By

[英]C# List Order By

我正在嘗試為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();

OrderBy和OrderByDescending不會修改列表,但會返回一個新的已排序元素列表。 您必須重新分配您的屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM