Comparing elements of List<object[]> with List<string>

ankit goel 766 Reputation points
2022-12-08T09:24:02.37+00:00

@Jack J Jun , I am working on the same program where i have to compare the list of strings with list of objects. Both the list of objects and list of string contains data in the following format.

268563-query-8-dec.jpg

268446-query2-8-dec.jpg

I am trying to compare all the names in the list of strings(defined as list in my example) with the names in the index[0] of each object of list of objects(listnew in my case) . If the name is found at the index[0] do nothing and if not add that name to the list of objects.

i cannot add the full code here so i use the text file
268486-query8dec.txt

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

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-12-08T09:59:33.013+00:00

    @ankit goel , Welcome to Microsoft Q&A,

    I make some changes in my code.

    Here is a code example you could refer to.

    var result1 = listnew.Select(m => m[0]).ToList();  
            var result = result1.Except(list);  
            listnew.RemoveAll(m => result.Contains(m[0]));  
            for (int i = 0; i < list.Count; i++)  
            {  
                if (!result1.Contains(list[i]))  
                {  
                    listnew.Add(new object[] { list[i],0 });  
                }  
    
            }  
            foreach (var item in listnew)  
            {  
                Console.WriteLine(item[0] + "**" +item[1]);  
            }  
    

    Tested result:

    268766-image.png
    Hope this could help you.
    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 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.