List中基于Dictionary Key-Value的OrderBy
OrderBy in List on the basis of Dictionary Key-Value
我有以下列表:
List<Student> list =
[
{
"id": 1,
"name": "Student 1",
"OtherInformation": {
"hobby": "Music",
"Score": 50
}
},
{
"id": 2,
"name": "Student 2",
"OtherInformation": {
"hobby": "Golf",
"Score": 70
}
},
{
"id": 3,
"name": "Student 3",
"OtherInformation": {
"hobby": "Archery",
"Score": 30
}
}
]
Other Information
是一个字典,我需要 OrderBy
使用字典值的完整列表,它将是动态的,即存储在某个变量中。
var sortKey = "id";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
我无法为 Score
等字典键值执行上述解决方案
Student.cs
public class Student
{
public string Id {get;set;}
public string Name {get;set;}
public IDictionary<string, object> OtherInfomration{get;set;}
}
需要帮助。
我使用这些数据制作了一个迷你控制台应用程序:
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student() { Id = 1, Name = "Student 1", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Music" },
{ "Score", "50" }
}
},
new Student() { Id = 2, Name = "Student 2", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Golf" },
{ "Score", "70" }
}
},
new Student() { Id = 3, Name = "Student 3", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Archery" },
{ "Score", "30" }
}
}
};
Console.WriteLine(list.OrderBy(x => x.OtherInformation["Score"]).FirstOrDefault().Name);
Console.Read();
}
}
成绩最低的学生3如愿
编辑:
在这里您可以使用预定义的订单语句:
public class OrderByStatment
{
private string propertyName;
public OrderByStatment(string propertyName)
{
this.propertyName = propertyName;
}
public Expression<Func<Student, object>> GetOrderBy()
{
switch (this.propertyName)
{
case "id": return s => s.Id;
case "name": return s => s.Name;
case "score": return s => s.OtherInformation["Score"];
case "hobby": return s => s.OtherInformation["hobby"];
default: return s => s.Id;
}
}
}
届时将调用它:
Console.WriteLine(list.OrderBy(new OrderByStatment("id").GetOrderBy().Compile()).FirstOrDefault().Name);
Console.WriteLine(list.OrderBy(new OrderByStatment("score").GetOrderBy().Compile()).FirstOrDefault().Name);
你绝对可以通过定义继承来让代码变得更好。您可以使用 new IdOrderBy()
而不是 new OrderByStatment("[propertyName]")
希望能回答您的问题
我想你想这样做:
var sortKey = "OtherInformation";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => ((IDictionary<string, object>)propertyInfo.GetValue(x, null))["Score"]).ToList();
然而,您使用反射的原因并不完全清楚。你可以很容易地做到这一点:
list = list.OrderBy(x => x.OtherInformation["Score"]).ToList();
你可以这样做:
var sorted = list.OrderBy(x => Convert.ToInt32(x.OtherInformation["Score"]));
Convert.ToInt32 有点狡猾,因为它会抛出异常。
我有以下列表:
List<Student> list =
[
{
"id": 1,
"name": "Student 1",
"OtherInformation": {
"hobby": "Music",
"Score": 50
}
},
{
"id": 2,
"name": "Student 2",
"OtherInformation": {
"hobby": "Golf",
"Score": 70
}
},
{
"id": 3,
"name": "Student 3",
"OtherInformation": {
"hobby": "Archery",
"Score": 30
}
}
]
Other Information
是一个字典,我需要 OrderBy
使用字典值的完整列表,它将是动态的,即存储在某个变量中。
var sortKey = "id";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
我无法为 Score
Student.cs
public class Student
{
public string Id {get;set;}
public string Name {get;set;}
public IDictionary<string, object> OtherInfomration{get;set;}
}
需要帮助。
我使用这些数据制作了一个迷你控制台应用程序:
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student() { Id = 1, Name = "Student 1", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Music" },
{ "Score", "50" }
}
},
new Student() { Id = 2, Name = "Student 2", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Golf" },
{ "Score", "70" }
}
},
new Student() { Id = 3, Name = "Student 3", OtherInformation = new Dictionary<string, string>()
{
{ "hobby", "Archery" },
{ "Score", "30" }
}
}
};
Console.WriteLine(list.OrderBy(x => x.OtherInformation["Score"]).FirstOrDefault().Name);
Console.Read();
}
}
成绩最低的学生3如愿
编辑:
在这里您可以使用预定义的订单语句:
public class OrderByStatment
{
private string propertyName;
public OrderByStatment(string propertyName)
{
this.propertyName = propertyName;
}
public Expression<Func<Student, object>> GetOrderBy()
{
switch (this.propertyName)
{
case "id": return s => s.Id;
case "name": return s => s.Name;
case "score": return s => s.OtherInformation["Score"];
case "hobby": return s => s.OtherInformation["hobby"];
default: return s => s.Id;
}
}
}
届时将调用它:
Console.WriteLine(list.OrderBy(new OrderByStatment("id").GetOrderBy().Compile()).FirstOrDefault().Name);
Console.WriteLine(list.OrderBy(new OrderByStatment("score").GetOrderBy().Compile()).FirstOrDefault().Name);
你绝对可以通过定义继承来让代码变得更好。您可以使用 new IdOrderBy()
而不是 new OrderByStatment("[propertyName]")希望能回答您的问题
我想你想这样做:
var sortKey = "OtherInformation";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => ((IDictionary<string, object>)propertyInfo.GetValue(x, null))["Score"]).ToList();
然而,您使用反射的原因并不完全清楚。你可以很容易地做到这一点:
list = list.OrderBy(x => x.OtherInformation["Score"]).ToList();
你可以这样做:
var sorted = list.OrderBy(x => Convert.ToInt32(x.OtherInformation["Score"]));
Convert.ToInt32 有点狡猾,因为它会抛出异常。