Linq 对象OrderBy对象属性-属性字符串配置实现
Linq 对象OrderBy对象属性-属性字符串配置实现
class Person
{
public string Name { get; set; }
public string Department { get; set; }
public string Age { get; set; }
public string Address { get; set; }
}
class Program
{
static void Main(string[] args)
{
object o = new object();
List<Person> persons = new List<Person>();
persons.Add(new Person(){Name="def"});
persons.Add(new Person() { Name = "aef" });
var ps = from p in persons
orderby GetInfo(p,"Name")
select p;
foreach (Person p in ps)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
static object GetInfo(Person p, string name)
{
return p.GetType().GetProperty(name).GetValue(p, null);
}
}
{
public string Name { get; set; }
public string Department { get; set; }
public string Age { get; set; }
public string Address { get; set; }
}
class Program
{
static void Main(string[] args)
{
object o = new object();
List<Person> persons = new List<Person>();
persons.Add(new Person(){Name="def"});
persons.Add(new Person() { Name = "aef" });
var ps = from p in persons
orderby GetInfo(p,"Name")
select p;
foreach (Person p in ps)
{
Console.WriteLine(p.Name);
}
Console.ReadKey();
}
static object GetInfo(Person p, string name)
{
return p.GetType().GetProperty(name).GetValue(p, null);
}
}