繁体   English   中英

基于Dictionary Key-Value的List中的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类的字典键值执行上述解决方案

学生.cs

 public class Student
{
 public string Id {get;set;}
 public string Name {get;set;}
 public IDictionary<string, object> OtherInfomration{get;set;}
}

需要帮助。

我想你想这样做:

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();

我使用这些数据制作了一个迷你控制台应用程序:

 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 sorted = list.OrderBy(x => Convert.ToInt32(x.OtherInformation["Score"]));

Convert.ToInt32 有点狡猾,因为它会抛出异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM