467,859 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,859 developers. It's quick & easy.

Check between List and Dictionary

Hi all,
having a List<stringand Dictionary<int, stringhow 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
--
Luigi

Sep 22 '08 #1
  • viewed: 2077
Share:
5 Replies
Luigi was thinking very hard :
Hi all,
having a List<stringand Dictionary<int, stringhow 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
Sep 22 '08 #2
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
Sep 22 '08 #3
"Hans Kesting" wrote:
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
Sep 22 '08 #4
Luigi <ci****************@inwind.itwrote:
having a List<stringand Dictionary<int, stringhow 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<stringfrom the List<stringand then iterate through
the dictionary values, checking that each value is in the set.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 22 '08 #5
OD
Hi all,
having a List<stringand Dictionary<int, stringhow 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
If lists are not too bigs you can use Linq to Object.
There're plenty of ways to get the results.
Here's one "sliced" and commented to be readable (shortests solutions
exist but it's just to show you a sample ) :
static void Main(string[] args)
{

var list = new List<string{"one", "two", "three"};
var dic = new Dictionary<int, string{{1, "one"}, {2,
"two"}, {3, "three"}, {4, "four"}};

// create a List<stringform the dictionnary
var q = (from KeyValuePair<int, stringk in dic select
k.Value).ToList();
// add List<stringitems to this query
q.AddRange(list);
// group all strings
var q2 = from s in q group s by s into z select z;
// select group where count is not equal to 2
q2 = from k in q2 where k.Count() != 2 select k;

Console.WriteLine("--- non matching items---");

foreach (var s in q2)
{
Console.WriteLine("item: "+s.Key + " - count: " +
s.Count());
}

// if q2 count differs from zero, then the sources are not
matching
Console.WriteLine(q2.Count()==0?"Match":"No match");
Console.ReadLine();

}

--
OD___
www.e-naxos.com
Sep 22 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by micklee74 | last post: by
3 posts views Thread by erik gartz | last post: by
7 posts views Thread by Sehboo | last post: by
7 posts views Thread by Andrew Robinson | last post: by
22 posts views Thread by SmokeWilliams | last post: by
20 posts views Thread by Seongsu Lee | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.