這裏以Linq爲例的對象。 ToList
確實沒有改變列表的排序。
List<CableList> Runlist = new List<CableList>
{
new CableList { Location = new Location
{ Facility = new Facility { FacilityTitle = "titel3" }}},
new CableList { Location = new Location
{ Facility = new Facility { FacilityTitle = "titel2" }}},
new CableList { Location = new Location
{ Facility = new Facility { FacilityTitle = "titel5" }}},
new CableList { Location = new Location
{ Facility = new Facility { FacilityTitle = "titel1" }}},
new CableList { Location = new Location
{ Facility = new Facility { FacilityTitle = "titel9" }}}
};
System.Diagnostics.Debug.WriteLine("Before order:");
foreach (var itm in Runlist)
System.Diagnostics.Debug.WriteLine(itm.Location.Facility.FacilityTitle);
var orderedResult = Runlist.OrderBy(x => x.Location.Facility.FacilityTitle)
.Skip(1)
.Take(4)
.ToList();
System.Diagnostics.Debug.WriteLine("After order:");
foreach (var itm in orderedResult)
System.Diagnostics.Debug.WriteLine(itm.Location.Facility.FacilityTitle);
輸出:
Before order:
titel3
titel2
titel5
titel1
titel9
After order:
titel2
titel3
titel5
titel9
來源
2016-12-02 14:30:43
dee
那是什麼'OrderBy'函數的輸出? – RandomStranger
你把它分配給任何東西嗎?它會創建一個新的列表。 – juharr
您是否將您的列表對象與它是實例的類相同?你確定位置和設施是否爲空?如果這是Linq to Sql(EF),則應該添加Include以檢索Location和Facility。 – Max