GroupBy in List of items using LINQ

69 views Asked by At

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?

2

There are 2 answers

4
Johnathan Barclay On BEST ANSWER

You can use Regex to extract the key:

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

If you believe your values will be consistent, you can try the below:

var groupedLanguages = languages.GroupBy(c => c.Substring(0, c.LastIndexOf('.')));

That will group by: "model.Languages[xx]"