Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. List<T> Comparison [SOLVED]

List<T> Comparison [SOLVED]

Scheduled Pinned Locked Moved C#
regex
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paladin2000
    wrote on last edited by
    #1

    I have two methods used to compate Lists, and I would like to combine them if possible. The purpose is to ensure that all elements in two lists match.

    private bool matchList_DateTime(List List1, List List2)
    {
    if (List1.Count != List2.Count) return false;
    foreach (DateTime L1 in List1) if (!List2.Contains(L1)) return false;
    return true;
    }

    private bool matchList_String(List List1, List List2)
    {
    if (List1.Count != List2.Count) return false;
    foreach (string L1 in List1) if (!List2.Contains(L1)) return false;
    return true;
    }

    modified on Tuesday, June 15, 2010 4:29 PM

    D L P K 4 Replies Last reply
    0
    • P Paladin2000

      I have two methods used to compate Lists, and I would like to combine them if possible. The purpose is to ensure that all elements in two lists match.

      private bool matchList_DateTime(List List1, List List2)
      {
      if (List1.Count != List2.Count) return false;
      foreach (DateTime L1 in List1) if (!List2.Contains(L1)) return false;
      return true;
      }

      private bool matchList_String(List List1, List List2)
      {
      if (List1.Count != List2.Count) return false;
      foreach (string L1 in List1) if (!List2.Contains(L1)) return false;
      return true;
      }

      modified on Tuesday, June 15, 2010 4:29 PM

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      An ideal candidate for generics

      bool CompareLists<T>(List<T> listA, List<T> listB)
      {
      if (listA.Count != listB.Count)
      return false;
      foreach (T item in listA)
      if (!listB.Contains(item))
      return false;
      return true;
      }

      Edit: I haven't optimised your code at all - just converted it to accept generics. You should probably null check both parameters - if both are null it's up to you how you want it to return. As this is nothing really to do with an actual class it may be useful to make it static and possibly an extension method if using 3.0 or above.

      Dave

      If this helped, please vote & accept answer!

      Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      P 1 Reply Last reply
      0
      • D DaveyM69

        An ideal candidate for generics

        bool CompareLists<T>(List<T> listA, List<T> listB)
        {
        if (listA.Count != listB.Count)
        return false;
        foreach (T item in listA)
        if (!listB.Contains(item))
        return false;
        return true;
        }

        Edit: I haven't optimised your code at all - just converted it to accept generics. You should probably null check both parameters - if both are null it's up to you how you want it to return. As this is nothing really to do with an actual class it may be useful to make it static and possibly an extension method if using 3.0 or above.

        Dave

        If this helped, please vote & accept answer!

        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        P Offline
        P Offline
        Paladin2000
        wrote on last edited by
        #3

        That works, thank you. I was trying to find a way to pass them as IEquatables, which wasn't working out.

        L 1 Reply Last reply
        0
        • P Paladin2000

          I have two methods used to compate Lists, and I would like to combine them if possible. The purpose is to ensure that all elements in two lists match.

          private bool matchList_DateTime(List List1, List List2)
          {
          if (List1.Count != List2.Count) return false;
          foreach (DateTime L1 in List1) if (!List2.Contains(L1)) return false;
          return true;
          }

          private bool matchList_String(List List1, List List2)
          {
          if (List1.Count != List2.Count) return false;
          foreach (string L1 in List1) if (!List2.Contains(L1)) return false;
          return true;
          }

          modified on Tuesday, June 15, 2010 4:29 PM

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          As a general List comparator, your method is flawed in one or two ways: 1. the order of items is disregarded (you may want to describe what "matching lists" is meant to mean); 2. when List1 holds duplicates, your method may return true even when List2 is not at all equivalent (even holding items that are not present in List1, or different quantities of List1 items). :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          P 1 Reply Last reply
          0
          • L Luc Pattyn

            As a general List comparator, your method is flawed in one or two ways: 1. the order of items is disregarded (you may want to describe what "matching lists" is meant to mean); 2. when List1 holds duplicates, your method may return true even when List2 is not at all equivalent (even holding items that are not present in List1, or different quantities of List1 items). :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            P Offline
            P Offline
            Paladin2000
            wrote on last edited by
            #5

            Good points. In this case, both lists contain distinct values (no duplicates). The order is insignificant, the point of the method is to determine if all items in List A exist in List B, and no others.

            realJSOPR 1 Reply Last reply
            0
            • P Paladin2000

              That works, thank you. I was trying to find a way to pass them as IEquatables, which wasn't working out.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Timothy CIAN wrote:

              I was trying to find a way to pass them as IEquatables, which wasn't working out.

              Why that?Perhaps using generic type constraints will help. :)

              Life is a stage and we are all actors!

              1 Reply Last reply
              0
              • P Paladin2000

                I have two methods used to compate Lists, and I would like to combine them if possible. The purpose is to ensure that all elements in two lists match.

                private bool matchList_DateTime(List List1, List List2)
                {
                if (List1.Count != List2.Count) return false;
                foreach (DateTime L1 in List1) if (!List2.Contains(L1)) return false;
                return true;
                }

                private bool matchList_String(List List1, List List2)
                {
                if (List1.Count != List2.Count) return false;
                foreach (string L1 in List1) if (!List2.Contains(L1)) return false;
                return true;
                }

                modified on Tuesday, June 15, 2010 4:29 PM

                P Offline
                P Offline
                Paladin2000
                wrote on last edited by
                #7

                Revised version...

                /// <summary>
                /// Compares two lists to ensure that they match without duplication.
                /// </summary>
                /// <typeparam name="T">Generic Type; string, DateTime, int etc.</typeparam>
                /// <param name="List1">First list to compare.</param>
                /// <param name="List2">Second list to compare.</param>
                /// <returns>
                /// True if the collections match and do not contain duplicates (regardless of sequence).
                /// False if either list is null, or they do not match.
                /// </returns>
                private static bool matchList<T>(List<T> List1, List<T> List2)
                {
                if (List1 == null || List2 == null) return false;
                if ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()))
                return true;
                return false;
                }

                L P 2 Replies Last reply
                0
                • P Paladin2000

                  Revised version...

                  /// <summary>
                  /// Compares two lists to ensure that they match without duplication.
                  /// </summary>
                  /// <typeparam name="T">Generic Type; string, DateTime, int etc.</typeparam>
                  /// <param name="List1">First list to compare.</param>
                  /// <param name="List2">Second list to compare.</param>
                  /// <returns>
                  /// True if the collections match and do not contain duplicates (regardless of sequence).
                  /// False if either list is null, or they do not match.
                  /// </returns>
                  private static bool matchList<T>(List<T> List1, List<T> List2)
                  {
                  if (List1 == null || List2 == null) return false;
                  if ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()))
                  return true;
                  return false;
                  }

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  An interesting approach. thanks. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  1 Reply Last reply
                  0
                  • P Paladin2000

                    Revised version...

                    /// <summary>
                    /// Compares two lists to ensure that they match without duplication.
                    /// </summary>
                    /// <typeparam name="T">Generic Type; string, DateTime, int etc.</typeparam>
                    /// <param name="List1">First list to compare.</param>
                    /// <param name="List2">Second list to compare.</param>
                    /// <returns>
                    /// True if the collections match and do not contain duplicates (regardless of sequence).
                    /// False if either list is null, or they do not match.
                    /// </returns>
                    private static bool matchList<T>(List<T> List1, List<T> List2)
                    {
                    if (List1 == null || List2 == null) return false;
                    if ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()))
                    return true;
                    return false;
                    }

                    P Offline
                    P Offline
                    Paladin2000
                    wrote on last edited by
                    #9

                    Revision revised... ;) I realized that I could simply return the comparison, as it is bool.

                    /// <summary>
                    /// Compares two lists to ensure that they match without duplication.
                    /// </summary>
                    /// <typeparam name="T">Generic Type; string, DateTime, int etc.</typeparam>
                    /// <param name="List1">First list to compare.</param>
                    /// <param name="List2">Second list to compare.</param>
                    /// <returns>
                    /// True if the collections match and do not contain duplicates (regardless of sequence).
                    /// False if either list is null, or they do not match.
                    /// </returns>
                    private static bool matchList<T>(List<T> List1, List<T> List2)
                    {
                    if (List1 == null || List2 == null) return false;
                    return ((List1.Count == List2.Count) && (List1.Count == List1.Intersect(List2).Count()));
                    }

                    1 Reply Last reply
                    0
                    • P Paladin2000

                      Good points. In this case, both lists contain distinct values (no duplicates). The order is insignificant, the point of the method is to determine if all items in List A exist in List B, and no others.

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      Does it matter if the contents of those items are also identical?

                      .45 ACP - because shooting twice is just silly
                      -----
                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                      1 Reply Last reply
                      0
                      • P Paladin2000

                        I have two methods used to compate Lists, and I would like to combine them if possible. The purpose is to ensure that all elements in two lists match.

                        private bool matchList_DateTime(List List1, List List2)
                        {
                        if (List1.Count != List2.Count) return false;
                        foreach (DateTime L1 in List1) if (!List2.Contains(L1)) return false;
                        return true;
                        }

                        private bool matchList_String(List List1, List List2)
                        {
                        if (List1.Count != List2.Count) return false;
                        foreach (string L1 in List1) if (!List2.Contains(L1)) return false;
                        return true;
                        }

                        modified on Tuesday, June 15, 2010 4:29 PM

                        K Offline
                        K Offline
                        Kevin McFarlane
                        wrote on last edited by
                        #11

                        If you're using .NET 3.5 or higher there's IEnumerable.SequenceEqual[^]

                        Kevin

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups