Get specific property from all items from the list

前端 未结 2 368
春和景丽
春和景丽 2020-12-15 04:27

I have list of Contacts:

public class Contact
{
    private string _firstName;
    private string _lastName;
    private int _age;

    /// 
         


        
2条回答
  •  渐次进展
    2020-12-15 05:00

    You want to use the Select method, not Where here:

    _contactList.Select(C => C.FirstName).ToList();
    

    Further, the need for the ToList() only exists because the property demands it. You could return an IEnumerable instead if you wanted to get rid of that.

提交回复
热议问题