C#: How to convert a list of objects to a list of a single property of that object?

前端 未结 6 1782
失恋的感觉
失恋的感觉 2020-11-30 02:31

Say I have:

IList people = new List();

And the person object has properties like FirstName, LastName, and Gende

6条回答
  •  渐次进展
    2020-11-30 03:20

    This will turn it to a list:

    List firstNames = people.Select(person => person.FirstName).ToList();
    

    This will return one (the first one):

    var firstname = people.select(e => e.firstname).FirstOrDefault();
    

提交回复
热议问题