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