根据 Properties/Fields 对不同 Objects 的列表进行动态排序的函数
Function To Dynamically Sort A List Of Different Objects By Their Properties/Fields
我的目标是让用户能够按名称
的属性/字段对不同 objects 的列表进行排序
(这些列表将显示在带有 headers 的 table 中,他们可以单击以对它们进行排序)
我不确定如何在这一行中动态指定 property/field 的类型
Expression.Lambda<Func<T, int>>(
我已经了解了下面的代码,但是当我按名称排序时显然失败了,因为它不是一个整数。
class Person
{
public int Id;
public string Name;
public Person(int id, string name)
{
Id = id;
Name = name;
}
}
class Car
{
public int Id;
public DateTime Manufactured;
public Car(int id, DateTime manufactured)
{
Id = id;
Manufactured = manufactured;
}
}
class Program
{
static void Main(string[] args)
{
//Create list of people
List<Person> listOfPeople = new List<Person>
{
new Person(1,"Nick"),
new Person(2,"Barry")
};
//Sort people by Id
List<Person> peopeSortedById =
DynamicSort(
propertyOrFieldName: "Id",
list: listOfPeople);
//Sort people by Name
List<Person> peopleSortedByName =
DynamicSort(
propertyOrFieldName: "Name",
list: listOfPeople);
//Create list of cars
List<Car> listOfCars = new List<Car>
{
new Car(1,DateTime.Now),
new Car(2,DateTime.Now)
};
//Sort cars by Id
List<Car> carsSortedById =
DynamicSort(
propertyOrFieldName: "Id",
list: listOfCars);
//Sort cars by Name
List<Car> carsSortedByName =
DynamicSort(
propertyOrFieldName: "Name",
list: listOfCars);
Console.Read();
}
public static List<T> DynamicSort<T>(
string propertyOrFieldName,
List<T> list)
{
var objectType =
Expression.Parameter(typeof(T));
var propertyOrField =
Expression.PropertyOrField(
expression: objectType,
propertyOrFieldName: propertyOrFieldName);
var propertyOrFieldType =
propertyOrField.Type;
var expression =
Expression.Lambda<Func<T, int>>(
body: propertyOrField,
parameters: objectType)
.Compile(); ;
return list.AsQueryable().OrderBy(expression).ToList();
}
}
我认为您不需要使用表达式 api。我建议像这样使用反射:
private IEnumerable<T> OrderByProperty<T>(IEnumerable<T> list, string propertyName)
=> list.OrderBy(x => typeof(T).GetProperty(propertyName).GetValue(x));
你可以像这样使用这个方法:
var orderedList = OrderByProperty(toBeOrdered, nameof(FooA.PropertyName));
如果您想使用表达式 Api,请确保创建 Expression,其中 R 是所选 属性 的类型。请注意,您不需要编译表达式。
我的目标是让用户能够按名称
的属性/字段对不同 objects 的列表进行排序(这些列表将显示在带有 headers 的 table 中,他们可以单击以对它们进行排序)
我不确定如何在这一行中动态指定 property/field 的类型
Expression.Lambda<Func<T, int>>(
我已经了解了下面的代码,但是当我按名称排序时显然失败了,因为它不是一个整数。
class Person
{
public int Id;
public string Name;
public Person(int id, string name)
{
Id = id;
Name = name;
}
}
class Car
{
public int Id;
public DateTime Manufactured;
public Car(int id, DateTime manufactured)
{
Id = id;
Manufactured = manufactured;
}
}
class Program
{
static void Main(string[] args)
{
//Create list of people
List<Person> listOfPeople = new List<Person>
{
new Person(1,"Nick"),
new Person(2,"Barry")
};
//Sort people by Id
List<Person> peopeSortedById =
DynamicSort(
propertyOrFieldName: "Id",
list: listOfPeople);
//Sort people by Name
List<Person> peopleSortedByName =
DynamicSort(
propertyOrFieldName: "Name",
list: listOfPeople);
//Create list of cars
List<Car> listOfCars = new List<Car>
{
new Car(1,DateTime.Now),
new Car(2,DateTime.Now)
};
//Sort cars by Id
List<Car> carsSortedById =
DynamicSort(
propertyOrFieldName: "Id",
list: listOfCars);
//Sort cars by Name
List<Car> carsSortedByName =
DynamicSort(
propertyOrFieldName: "Name",
list: listOfCars);
Console.Read();
}
public static List<T> DynamicSort<T>(
string propertyOrFieldName,
List<T> list)
{
var objectType =
Expression.Parameter(typeof(T));
var propertyOrField =
Expression.PropertyOrField(
expression: objectType,
propertyOrFieldName: propertyOrFieldName);
var propertyOrFieldType =
propertyOrField.Type;
var expression =
Expression.Lambda<Func<T, int>>(
body: propertyOrField,
parameters: objectType)
.Compile(); ;
return list.AsQueryable().OrderBy(expression).ToList();
}
}
我认为您不需要使用表达式 api。我建议像这样使用反射:
private IEnumerable<T> OrderByProperty<T>(IEnumerable<T> list, string propertyName)
=> list.OrderBy(x => typeof(T).GetProperty(propertyName).GetValue(x));
你可以像这样使用这个方法:
var orderedList = OrderByProperty(toBeOrdered, nameof(FooA.PropertyName));
如果您想使用表达式 Api,请确保创建 Expression