在C#.NET MVC中实现IEqualityComparer [英] Implementing IEqualityComparer in C# .NET MVC

查看:147
本文介绍了在C#.NET MVC中实现IEqualityComparer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个自定义比较器,如下所示:

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...

用法示例:

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

用法示例和预期结果:

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

要添加到thirdList列表中的项目的条件如下:

Where the criteria for the items to be added to thirdList list are following:

  • 如果newList中的ItemID不存在于oldList中
  • 如果两个列表中都存在两个ItemID,但其QuantitySold属性不相等...

我可以使用一个IEqualityComparer实现此功能吗?有人可以帮我吗?

Can I implement this using one IEqualityComparer? Can someone help me out?

@CodingYoshi,会这样吗?

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

推荐答案

我写了一个扩展方法,可以为您完成所需的行为.

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.

代码:

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

用法:

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

这篇关于在C#.NET MVC中实现IEqualityComparer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆