public class Student
{
public string First { get; set; }
public string Last { get; set; }
public int ID { get; set; }
}
public static List<Student> GetStudents()
{
// Use a collection initializer to create the data source. Note that each element
// in the list contains an inner sequence of scores.
List<Student> students = new List<Student>
{
new Student {First="Svetlana", Last="Omelchenko", ID=111},
new Student {First="Claire", Last="O'Donnell", ID=112},
new Student {First="Sven", Last="Mortensen", ID=113},
new Student {First="Cesar", Last="Garcia", ID=114},
new Student {First="Debra", Last="Garcia", ID=115}
};
return students;
}
GetStudents().OrderBy(u => u.First)
GetStudents().OrderBy(u => u.Last)
GetStudents().OrderBy(u => u.ID)
可以排序,如果被排序的欄位是個變數,應該怎么寫呢?
如 string field="First";
GetStudents().OrderBy(u => u.?)
uj5u.com熱心網友回復:
使用反射(Reflection)var dynamicPropFromStr = typeof(Student).GetProperty("First");
GetStudents().OrderBy(u=>dynamicPropFromStr.GetValue(u, null))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/195537.html
標籤:C#
