列表上對象的列表兩個列表像C#:如何排序基於我有串
List<String> l_lstNames = new List<String> { "A1", "A3", "A2", "A4", "A0" };
List<Test> l_lstStudents = new List<Test>
{ new Test { Age = 20, Name = "A0" },
new Test { Age = 21, Name = "A1" },
new Test { Age = 22, Name = "A2" },
new Test { Age = 23, Name = "A3" },
new Test { Age = 24, Name = "A4" },
};
哪裏Test
是類像
public class Test
{
public String Name;
public Int32 Age;
}
我需要對項目進行排序在l_lstStudents
的基礎上l_lstNames
。因此,排序列表就會像,
List<Test> l_lstStudents = new List<Test>
{ new Test { Age = 21, Name = "A1" },
new Test { Age = 23, Name = "A3" },
new Test { Age = 22, Name = "A2" },
new Test { Age = 24, Name = "A4" },
new Test { Age = 20, Name = "A0" },
};
現在,我使用for
做到這一點。
像
創建
Test
對象的新名單。迭代
l_lstNames
的循環並從l_lstStudent
中獲取Test
對象,並將其添加到新創建的列表中。最後,新的列表分配給l_lstStudent
請幫我做一個簡單的方法(LINQ或λ)
我知道你說的簡單,但你更關心的是性能或幾行的代碼? – Tung 2012-03-09 11:45:00
[C#排序列表基於另一個列表]的可能重複(http://stackoverflow.com/questions/3355928/c-sharp-sort-list-based-on-another-list) – nawfal 2013-10-15 15:07:49