List<T> Comparison [SOLVED]
-
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
-
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
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) -
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)That works, thank you. I was trying to find a way to pass them as IEquatables, which wasn't working out.
-
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
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).
-
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).
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.
-
That works, thank you. I was trying to find a way to pass them as IEquatables, which wasn't working out.
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!
-
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
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;
} -
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;
}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).
-
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;
}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()));
} -
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.
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 -
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
If you're using .NET 3.5 or higher there's IEnumerable.SequenceEqual[^]
Kevin