0

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.

4
  • Welcome to StackOverflow. What have you tried so far ? Commented Nov 27, 2022 at 11:47
  • I tried this which i share, i dont know how to do it, i am new, please help Commented Nov 27, 2022 at 11:48
  • I suggest you first read this. People here respond negatively if your question gives them the impression that you're asking them to do your work for you. Commented Nov 27, 2022 at 11:52
  • Well by the way this is not home work question and i already told you i am new to stack overflow and also new to c# so i dont know how to do it, i am sure here are professional programmer and this one is easy question, please help me, i dont want to waste my more 5-10 hours again to try then again post here, i am trying from last 2-3 days please help Commented Nov 27, 2022 at 11:57

2 Answers 2

0

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot, very nice, clean and simple solution !!! You great man
0

Take a look at your output. Notice that an equal sign separates each string into a key-value pair. Think about how you want to approach this problem. Is a list of strings really the structure you want to build on? You could take a different approach and use a list of KeyValuePairs or a Dictionary instead.

If you really need to do it with a List, then look at the methods LINQ's Enumerable has to offer. Namely Select and GroupBy.

You can use Select to split once more on the equal sign: .Select(s => s.Split('=')).

You can use GroupBy to group values by a key: .GroupBy(pair => pair[0]).

To join it back to a string, you can use a Select again.

An end result could look something like this:

List<string> stringList = values.Split('&')
    .Select(s => {
        string[] pair = s.Split('=');
        return new { Key = pair[0], Value = pair[1] };
    })
    .GroupBy(pair => pair.Key)
    .Select(g => string.Concat(
        g.Key,
        '=',
        string.Join(
            ", ",
            g.Select(pair => pair.Value)
        )
    ))
    .ToList();

The group contains pairs so you need to select the value of each pair and join them into a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.