英文:
OrderBy in List on the basis of Dictionary Key-Value
问题
我有以下的列表:
List<Student> list =
[
{
"id": 1,
"name": "学生1",
"OtherInformation": {
"hobby": "音乐",
"Score": 50
}
},
{
"id": 2,
"name": "学生2",
"OtherInformation": {
"hobby": "高尔夫",
"Score": 70
}
},
{
"id": 3,
"name": "学生3",
"OtherInformation": {
"hobby": "射箭",
"Score": 30
}
}
]
Other Information
是一个字典,我需要使用字典中的动态值来对整个列表进行排序。
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; }
}
需要帮助。
英文:
I have the following List:
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
is a Dictionary and I need to OrderBy
the complete list using the dictionary Value which will be dynamic i.e. stored in some variable.
var sortKey = "id";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => propertyInfo.GetValue(x, null)).ToList();
I'm not able to do the above solution for Dictionary Key-Values such as Score
Student.cs
public class Student
{
public string Id {get;set;}
public string Name {get;set;}
public IDictionary<string, object> OtherInfomration{get;set;}
}
Help needed.
答案1
得分: 2
我认为您想要执行以下操作:
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();
英文:
I think you want to do this:
var sortKey = "OtherInformation";
var propertyInfo = typeof(Student).GetProperty(sortKey);
list = list.OrderBy(x => ((IDictionary<string, object>)propertyInfo.GetValue(x, null))["Score"]).ToList();
Its not entirely clear why you're using reflection however. You could easily just do this:
list = list.OrderBy(x => x.OtherInformation["Score"]).ToList();
答案2
得分: 0
这是使用这些数据制作的一个迷你控制台应用程序:
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 OrderByStatement
{
private string propertyName;
public OrderByStatement(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 OrderByStatement("id").GetOrderBy().Compile()).FirstOrDefault().Name);
Console.WriteLine(list.OrderBy(new OrderByStatement("score").GetOrderBy().Compile()).FirstOrDefault().Name);
您可以通过定义继承来更改代码,而不是使用 new OrderByStatement("[propertyName]")
,您可以使用 new IdOrderBy()
。
希望这回答了您的问题。
英文:
I made a mini console application using this data:
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();
}
}
The result was Student 3 as wanted because he has the lowest score.
EDIT:
Here you can use a pre-defined order statments:
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;
}
}
}
calling it will be then:
Console.WriteLine(list.OrderBy(new OrderByStatment("id").GetOrderBy().Compile()).FirstOrDefault().Name);
Console.WriteLine(list.OrderBy(new OrderByStatment("score").GetOrderBy().Compile()).FirstOrDefault().Name);
You can absolutely make the code better by defining inheritance. Instead of new OrderByStatment("[propertyName]") you can use new IdOrderBy()
Hope that answers your question
答案3
得分: 0
你可以这样做:
var sorted = list.OrderBy(x => Convert.ToInt32(x.OtherInformation["Score"]));
Convert.ToInt32 有点不稳定,因为它可能引发异常。
英文:
You can do this:
var sorted = list.OrderBy(x => Convert.ToInt32(x.OtherInformation["Score"]));
Convert.ToInt32 is a bit dodgy as it can throw an exception.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论