MatchList
-
/// <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()));
} -
/// <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()));
}Mmmm, so if the two Lists are null, they're not matching? Depends on your definition for sure.
-
Mmmm, so if the two Lists are null, they're not matching? Depends on your definition for sure.
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:
-
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:
Timothy CIAN wrote:
used in databases
In SQL Server
null != null
(as far as I recall). -
Timothy CIAN wrote:
used in databases
In SQL Server
null != null
(as far as I recall).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. -
/// <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()));
}