Sort a List using query expressions

后端 未结 7 679
执念已碎
执念已碎 2021-02-05 15:34

I have a problem using Linq to order a structure like this :

public class Person
{
    public int ID { get; set; }
    public List Attribu         


        
7条回答
  •  广开言路
    2021-02-05 16:00

    Why don't you use a key-value dictionary instead of your List ? It would suit better, i think, and make everything else easier.

    Update - like this:

    public class Person
    {
      public Dictionary Attributes = new Dictionary();
    }
    
    List people = new List();
    
    Person rebecca = new Person();
    rebecca.Attributes["Age"] = "32";
    rebecca.Attributes["FirstName"] = "Rebecca";
    rebecca.Attributes["LastName"] = "Johnson";
    rebecca.Attributes["Gender"] = "Female";
    people.Add(rebecca);
    
    var PeopleInAgeOrder = people.OrderBy(p => p.Attributes["Age"]);
    

提交回复
热议问题