Skip to content

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

Updated: at 02:12 PM

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.
Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects