从列表中的所有项目中获取特定属性

我有联系人列表:

public class Contact { private string _firstName; private string _lastName; private int _age; ///  /// Constructor ///  /// Contact's First Name /// Contact's Last Name /// Contact's Age public Contact(string fname, string lname, int age) { _firstName = fname; _lastName = lname; _age = age; } ///  /// Contact Last Name ///  public string LastName { get { return _lastName; } set { _lastName = value; } } ///  /// Contact First Name ///  public string FirstName { get { return _firstName; } set { _firstName = value; } } ///  /// Contact Age ///  public int Age { get { return _age; } set { _age = value; } } } 

在这里我创建我的列表:

 private List _contactList; _contactList = new List(); _contactList.Add(new Contact("John", "Jackson", 45)); _contactList.Add(new Contact("Jack", "Doe", 20)); _contactList.Add(new Contact("Jassy", "Dol", 19)); _contactList.Add(new Contact("Sam", "Josin", 44)); 

现在我试图使用LINQ获取所有联系人的所有名字在单独的列表中。

到目前为止我尝试过:

  public List FirstNames { get { return _contactList.Where(C => C.FirstName.ToList()); } } 

你想使用Select方法,而不是Where这里:

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

此外,仅存在对ToList()的需求,因为property需要它。 如果你想摆脱它,你可以返回一个IEnumerable

 public List FirstNames { get { return _contactList.Select(C => C.FirstName).ToList(); } }