Merge List<string> with List<object[]>

ankit goel 766 Reputation points
2022-12-14T11:55:03.817+00:00

@Jack J Jun ,I am trying to merge two lists together to create a fresh new list but i am out of idea how to merge both of them to create a new list. please suggest me the code .
list 1 declaration

  List<object[]> listnew = new List<object[]>();  

data in the list 1
270602-image.png

list 2 declaration

    List<string> list1 = new List<string>(); // just for sub-headings   

data in the list 2
270523-image.png

what i am trying to achieve is
270592-image.png

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-12-15T02:15:14.36+00:00

    @ankit goel , Welcome to Microsoft Q&A, you could try to define Dictionary<string, List<string>> to get the wanted data.

    Here is a code example you could refer to.

     Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();  
                for (int i = 0; i < list.Count; i++)  
                {  
                    string key = list[i];  
                    List<string> l = new List<string>();  
                    foreach (var item in listnew)  
                    {  
                        if (item[0].ToString().Contains(key))  
                        {  
                            l.Add(item[0].ToString().Replace(key, ""));  
                        }  
      
                    }  
                    dic.Add(key, l);  
                }  
    

    Result:

    270697-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.