1

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?

Arif
  • 6,094
  • 4
  • 49
  • 81

2 Answers2

1

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]"

Oyyou
  • 610
  • 5
  • 13
1

You can use Regex to extract the key:

var grouped = languages.GroupBy(l => Regex.Match(l, @"Languages\[\d+\]").Value);
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35