Home > Net >  Consult a collection sorting problem
Consult a collection sorting problem

Time:10-30

 
Public class Student
{
Public string First {get; set; }
Public string Last {get; set; }
Public int ID {get; set; }
}

Public static List GetStudents ()
{
//Use a collection, initializer to create the data source. Note that each element
//in the list contains an inner sequence of scores.
List Students=new List
{
The new Student {First="life", the 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=& gt; U.F irst)
GetStudents (.) OrderBy (u=& gt; U.L ast)
GetStudents (.) OrderBy (u=& gt; U.I D)

Can sort, if the sorting is a variable field, should be how to write?
If the string field="First";
GetStudents (.) OrderBy (u=& gt; U.?

CodePudding user response:

Using Reflection (Reflection)
 var dynamicPropFromStr=typeof (Student). GetProperty (" First "); 
GetStudents (.) OrderBy (u=& gt; DynamicPropFromStr. GetValue (u, null))
  •  Tags:  
  • C#
  • Related