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. Other Discussions
  3. Clever Code
  4. MatchList

MatchList

Scheduled Pinned Locked Moved Clever Code
regex
6 Posts 4 Posters 28 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

    /// <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>
    public 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()));
    }

    J E 2 Replies Last reply
    0
    • P Paladin2000

      /// <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>
      public 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()));
      }

      J Offline
      J Offline
      Julien Villers
      wrote on last edited by
      #2

      Mmmm, so if the two Lists are null, they're not matching? Depends on your definition for sure.

      P 1 Reply Last reply
      0
      • J Julien Villers

        Mmmm, so if the two Lists are null, they're not matching? Depends on your definition for sure.

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

        Well, that depends: there are two ways to define "null" that I know of. In some cases (and as commonly used in .NET), it literally means "nothing". If you use this version, than "nothing" could indeed match "nothing". However, it is also at times used to mean "not provided; undetermined". This interpretation of "null" is commonly used in databases. If this is the version used, it is not true that "I don't know" matches "I don't know". Like nailing pudding to the wall, useless. A good observation, and I did indicate the usage in the comments/documentation. :cool:

        P 1 Reply Last reply
        0
        • P Paladin2000

          Well, that depends: there are two ways to define "null" that I know of. In some cases (and as commonly used in .NET), it literally means "nothing". If you use this version, than "nothing" could indeed match "nothing". However, it is also at times used to mean "not provided; undetermined". This interpretation of "null" is commonly used in databases. If this is the version used, it is not true that "I don't know" matches "I don't know". Like nailing pudding to the wall, useless. A good observation, and I did indicate the usage in the comments/documentation. :cool:

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Timothy CIAN wrote:

          used in databases

          In SQL Server null != null (as far as I recall).

          P 1 Reply Last reply
          0
          • P PIEBALDconsult

            Timothy CIAN wrote:

            used in databases

            In SQL Server null != null (as far as I recall).

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

            That is correct, you have to set ANSI_NULLS off in SQL Server to have null = null.

            ANSI-92:
            null value (null): A special value, or mark, that is used to indicate the absence of any data value.

            1 Reply Last reply
            0
            • P Paladin2000

              /// <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>
              public 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()));
              }

              E Offline
              E Offline
              ekolis
              wrote on last edited by
              #6

              Recommendation: replace List with IEnumerable (you will need to add parens after .Count) - this way you can use this method to compare not just Lists but also other collection types, and even arrays!

              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