I got an object list with some attributes and after populating this list, I want to get all the elements from a specific variable of this object and put it inside a new INT list. I’ll try to example:
public class Obj1
{
public int id {get; set; }
public string name {get; set; }
public int? age {get; set; }
}
public List<int> GetAgeList(List<Obj1> list)
{
List<int> ageList = list.age;//The idea is add all ages into the list..
return ageList;
}
Thanks!
Mannimarco is right–you should have provided some of what you have attempted 🙂 There are a number of ways to do this. Maybe you should look up List (particularly, the extension method section) and start learning LINQ.
Use
.Select()extension method of the Generic List type. (I’m assumingobj1is the same type asObj1and you just made a typo)