We help IT Professionals succeed at work.

Comparing contents of two lists

I have two lists of strings:

List<string> list1 ...
List<string> list2 ...

I ned to verify they have the same strings in them, but sequence is not important.

Is there a function for this?? Better than my code, I mean:

Thanks!

 
foreach( string st in viewTagList)
            {
                if (xmlTagList.IndexOf(st) == -1)
                {
                    return false;
                }
            }
            foreach (string st in xmlTagList)
            {
                if (viewTagList.IndexOf(st) == -1)
                {
                    return false;
                }
            }

Open in new window

Comment
Watch Question

MisbahSr. Software Engineer

Commented:
try the below code, it uses Contains method.
foreach( string st in viewTagList)
{
	if (!xmlTagList.Contains(st))
	{
		return false;
	}

	
}

return true;

Open in new window

Sr. Software Engineer
Commented:
you can also try this one:

bool result = viewTagList.SequenceEqual(xmlTagList);

Open in new window

curiouswebsterSoftware Engineer

Author

Commented:
Thanks!