Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey All,

I've not done a massive amount with LINQ group by and I'm wondering if there is a better way than this:

C#
public static void CreateLookupTypesFromMasterLookupCollection()
{
    var groups = _lookupMasterCollection.GroupBy(x => x.TypeId).Select(x => x).ToList();
    foreach (var group in groups)
    {
        foreach (var item in group)
        {
            _lookupTypeMasterCollection.Add(new LookupTypeDto(item.Id, item.TypeName));
            break;
        }
    }
}


I just want one of each collection of lookup items (x is a hand rolled class called (LookupItem).

Cheers,
Posted
Updated 5-Jan-12 8:31am
v2

1 solution

C#
_lookupTypeMasterCollection.AddRange(_lookupMasterCollection.GroupBy(x => x.TypeId).Select(x => new LookupTypeDto(x.First().Id, x.First().TypeName)));
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900