简体   繁体   English

在C#.NET MVC中实现IEqualityComparer

[英]Implementing IEqualityComparer in C# .NET MVC

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: 要添加到thirdList列表中的项目的条件如下:

  • if ItemID in newList is not present in oldList 如果newList中的ItemID不存在于oldList中
  • If both ItemID's are present in both list but their QuantitySold properties are not equal... 如果两个列表中都存在两个ItemID,但它们的QuantitySold属性不相等...

Can I implement this using one IEqualityComparer? 我可以使用一个IEqualityComparer来实现吗? Can someone help me out? 有人可以帮我吗?

@CodingYoshi, will something like this do: @CodingYoshi,将执行以下操作:

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM