Split a List of structs into sublists based on the stucts contents

I have a List that I need to split into sublists, one for each value of MyStruct.GroupingString. Can I do this via linq?

Upvotes: 2

Views: 537

Answers (2)

leppie
leppie

Reputation: 117260

somelist.ToLookup(x => x.GroupingString)

Upvotes: 3

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422046

List<List<StructType>> groupings = list.GroupBy(x => x.GroupingString)
                                       .Select(x => x.ToList()).ToList();

Upvotes: 3

Related Questions