Check between List and Dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?THVpZ2k=?=

    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

  • Hans Kesting

    #2
    Re: Check between List and Dictionary

    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


    Comment

    • Marc Gravell

      #3
      Re: Check between List and Dictionary

      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.O rderBy(s=>s);

      bool areEqual = lookupQry.Seque nceEqual(listQr y);

      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

      Comment

      • =?Utf-8?B?THVpZ2k=?=

        #4
        Re: Check between List and Dictionary

        "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 (!tavolaColumns Dictionary.Cont ainsValue(colum nName))
        throw new NotMatchingColu mnsVoceTavola() ;
        }

        Thanks.

        Luigi

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Check between List and Dictionary

          Luigi <ciupazNoSpamGr azie@inwind.itw rote:
          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<stringf rom the List<stringand then iterate through
          the dictionary values, checking that each value is in the set.

          --
          Jon Skeet - <skeet@pobox.co m>
          Web site: http://www.pobox.com/~skeet
          Blog: http://www.msmvps.com/jon.skeet
          C# in Depth: http://csharpindepth.com

          Comment

          • OD

            #6
            Re: 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
            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{"on e", "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<in t, stringk in dic select
            k.Value).ToList ();
            // add List<stringitem s 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.WriteLi ne("--- non matching items---");

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

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


            Console.ReadLin e();

            }

            --


            OD___



            Comment

            Working...