How to Create a Generic Integer List From a List of Objects Using LINQ

Posted by Peter Kellner on June 03, 2009 · 1 min read

So, this is very straight forward, but I sometimes forget it.  I figure I’ll do a short blog post on it so next time I search for it, I’ll probably hit my own blog post.

So, say you have a list of objects as follows:

public partial class CompanyAddressResult : ResultBase
    {
        public int Id {get;set;}
        public string Name { get; set; }
        public int CompanyId { get; set; }
        public int AddressId { get; set; }
        public int CompanyAddressTypeId { get; set; }
        public string Notes { get; set; }
        public int? ActiveFlag { get; set; }
}
And, you want to create a list of AddressIds.  Here is all you have to do:
List<int> AddressIds = retList.Select(a => a.AddressId).ToList();
Hope this helps.