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
  • 4,274
  • 3
  • 40
  • 64

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
  • 436
  • 3
  • 12
1

You can use Regex to extract the key:

var grouped = languages.GroupBy(l => Regex.Match(l, @"Languages\[\d+\]").Value);
Johnathan Barclay
  • 14,082
  • 11
  • 26