Check between List and Dictionary

L

Luigi

Hi all,
having a List<string> and Dictionary<int, string> how can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

Thanks in advance
 
H

Hans Kesting

Luigi was thinking very hard :
Hi all,
having a List<string> and Dictionary<int, string> how can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

Thanks in advance

Something like:

1) check that the Counts are equal
2) foreach item in the first list, check (Contains/ContainsValue) that
is is present in the second list

This will fail if duplicate entries are allowed

or:
1) loop through all items in list1, check if it exists in list2 (if all
exists, then list1 is a subset of list2, but list2 might contain extra
items)
2) loop through all items in list2, check if it exists in list1 (if all
exists, then the lists are identical)


Hans Kesting
 
L

Luigi

Hans Kesting said:
Something like:

1) check that the Counts are equal
2) foreach item in the first list, check (Contains/ContainsValue) that
is is present in the second list

This will fail if duplicate entries are allowed

or:
1) loop through all items in list1, check if it exists in list2 (if all
exists, then list1 is a subset of list2, but list2 might contain extra
items)
2) loop through all items in list2, check if it exists in list1 (if all
exists, then the lists are identical)

Hi Hans,
something like this?

foreach(string columnName in columnNamesList)
{
if (!tavolaColumnsDictionary.ContainsValue(columnName))
throw new NotMatchingColumnsVoceTavola();
}

Thanks.

Luigi
 
M

Marc Gravell

It depends on the size; one quick'n'dirty option would be to get the
two sets sorted the same, and use SequenceEqual:

var listQry = list.OrderBy(s=>s);
var lookupQry = lookup.Values.OrderBy(s=>s);

bool areEqual = lookupQry.SequenceEqual(listQry);

For larger volumes, then rather than sorting both, I would be tempted
to create a hashtable from the list, and just enumerate over the
dictionary Values checking. It is a sham you don't need to check the
dictionary keys (rather than values), as this would be a lot more
direct and efficient... are you sure the dictionary is the right way
around?

Marc
 
J

Jon Skeet [C# MVP]

Luigi said:
having a List<string> and Dictionary<int, string> how can I check that every
string in the list is also in the Dictionary (and viceversa)?
(and raise an exception when not).

What version of .NET are you using? If you're using .NET 3.5 I suggest
you first check that the counts are equal (to give an "early out")
build a HashSet<string> from the List<string> and then iterate through
the dictionary values, checking that each value is in the set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top