Fill list from entity collection

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 17-May-2012 06:02:57   

What would be the best way to fill a generic list from a field or fields in an entity collection? For example, I have a customer collection and I want to fill a HashSet<T> or List<T> with one field in the entity collection? Or a dictionary<T, T> with two fields from the collection?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-May-2012 06:29:08   

Something like:

var someList = new List<string>();          
someList.AddRange(
    (from c in customers
        select c.CompanyName));

var someDictionary = new Dictionary<string, string>();
foreach (var c in customers)
    someDictionary.Add(c.CustomerId, c.CompanyName);

var someHashset = new HashSet<string>();
foreach (var c in customers)
    someHashset.Add(c.CustomerId);
David Elizondo | LLBLGen Support Team