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

10 浏览
0 Comments

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

我有一个联系人列表:

public class Contact

{

private string _firstName;

private string _lastName;

private int _age;

public Contact(string fname, string lname, int age)

{

_firstName = fname;

_lastName = lname;

_age = age;

}

public string LastName

{

get

{

return _lastName;

}

set

{

_lastName = value;

}

}

public string FirstName

{

get

{

return _firstName;

}

set

{

_firstName = value;

}

}

public int Age

{

get

{

return _age;

}

set

{

_age = value;

}

}

}

现在我正在使用LINQ获取所有联系人的名字并将其放入一个单独的列表中。

我尝试过:

public List FirstNames

{

get

{

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

}

}

0