Print List of objects to Console

后端 未结 3 1297
刺人心
刺人心 2020-12-03 19:35

I created a list with Listobj object type. And added a set of values to the object.

How do I print the Listobj objects from the newlist in an increasing age fashion.

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 20:23

    You can do with using the IEnumerable varaible, and then sorting them in ascending order using linq

    IEnumerable temp = newlist;
    temp = from v in temp
           orderby v.age ascending
           select v;
    
    foreach (Listobj item in temp)
    {
        Console.WriteLine(item.Name +"with the age"+ item.Age);
    }
    
    Console.ReadLine();
    

提交回复
热议问题