Home > Mobile >  How to modify string list for duplicate values?
How to modify string list for duplicate values?

Time:11-27

I am working on project which is asp.net mvc core. I want to replace string list of duplicate values to one with comma separated,

List<string> stringList = surveylist.Split('&').ToList();

I have string list This generate following output:

7=55
6=33
5=MCC
4=GHI
3=ABC
1003=DEF
1003=ABC
1=JKL

And I want to change output like this

7=55
6=33
5=MCC
4=GHI
3=ABC
1003=DEF,ABC
1=JKL

Duplicate items values should be comma separated.

CodePudding user response:

There are probably 20 ways to do this. One simple one would be:

List<string> newStringList = stringList
  .Select(a => new { KeyValue = a.Split("=") })
  .GroupBy(a => a.KeyValue[0])
  .Select(a => $"{a.Select(x => x.KeyValue[0]).First()}={string.Join(",", a.Select(x => x.KeyValue[1]))}")
  .ToList();
  • Related