Reputation: 3823
I've implemented a custom comparer like following:
public class CustomComparer : IEqualityComparer<StoredEmails>
{
public static readonly IEqualityComparer<StoredEmails> Instance = new CustomComparer();
public bool Equals(StoredEmails x, StoredEmails y)
{
return x.Email == y.Email;
}
public int GetHashCode(StoredEmails x)
{
return x.Email.GetHashCode();
}
}
This one basically compares two lists (two equally same typed lists) and adds ONLY MISSING emails from that list to the new one and then I insert those emails to my database...
Usage example:
var oldList = new List<StoredEmails>(); // lets suppose it has 5000 emails or something like that, just for example's sake...
var ListDoAdd = prepared.Except(oldList, CustomComparer.Instance).ToList();
Where "prepared" list is the new one which is compared to the old list..
Now I'd like to implement this as well , just for different class and different criteria rules:
- Items class
Where I have two identically typed lists (old and new list) of items which contains following data:
- ItemID
- QuantitySold
Usage example and desired outcome:
var oldItems= new List<Items>(); // suppose it has 5000 items inside...
var newItems = new List<Items>(); // suppose it has 15000 items...
var thirdList = newItems .Except(oldItems, CustomComparer.Instance).ToList();
Where the criteria for the items to be added to thirdList list are following:
Can I implement this using one IEqualityComparer? Can someone help me out?
@CodingYoshi, will something like this do:
public static readonly IEqualityComparer<Items> Instance = new ItemsComparer();
public bool Equals(Items x, Items y)
{
if (x.ItemId != y.ItemId || x.QuantitySoldTotal != y.QuantitySoldTotal)
return true;
return false;
}
public int GetHashCode(Items x)
{
return x.ItemId.GetHashCode();
}
Upvotes: 0
Views: 401
Reputation: 526
I wrote an extension method that does the desired behavior for you.
If the new item is not in the old items list, it will be added to the result list. If the item was modified, it will also be added to the result list.
Code:
public static class CollectionExtensions
{
public static IList<T> GetNewOrModifiedItems<T, TKey>(
this IList<T> newItems,
IList<T> oldItems,
Func<T, TKey> getKey,
IEqualityComparer<T> comparer)
{
oldItems = oldItems ?? new List<T>();
newItems = newItems ?? new List<T>();
var oldItemsDictionary = oldItems.ToDictionary(getKey);
var results = new List<T>();
foreach (var item in newItems)
{
if (!oldItemsDictionary.ContainsKey(getKey(item)) ||
!comparer.Equals(item, oldItemsDictionary[getKey(item)]))
{
results.Add(item);
}
}
return results;
}
}
Usage:
var oldItems = new List<Items>(); // suppose it has 5000 items...
var newItems = new List<Items>(); // suppose it has 15000 items...
var thirdList = newItems.GetNewOrModifiedItems(
oldItems,
x => x.ItemId,
CustomComparer.Instance);
Upvotes: 1