Search code examples
c#linqlinq-method-syntax

GroupBy in List of items using LINQ


I have a list that contains item like below:

enter image description here

Now I want to group according to language index (Languages[x]) like below:

Group-1:
model.Languages[0].Employer
model.Languages[0].Title

Group-2:
model.Languages[1].Employer
model.Languages[1].Title

Can AnyBody tell me how can I do this?


Solution

  • You can use Regex to extract the key:

    var grouped = languages.GroupBy(l => Regex.Match(l, @"Languages\[\d+\]").Value);