Sort a List using query expressions

后端 未结 7 664
执念已碎
执念已碎 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:22

    Could it be that your syntax is wrong? Your property is called Attributes but your using something called ObjectSettings in code? Or is that a typo.

    If it is then your code looks fine unless not all Person instances have the Attribute you're trying to order by in which case you'd get an exception.

    EDIT: Also, instead of using Find, try using First.

    PersonList.OrderBy(p => p.Attributes.First(a => a.Name == "Age").Value)
    
    0 讨论(0)
提交回复
热议问题