【问题标题】:Implementing IEqualityComparer in C# .NET MVC在 C# .NET MVC 中实现 IEqualityComparer
【发布时间】:2017-04-24 20:10:43
【问题描述】:

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

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

这个基本上比较了两个列表(两个相同类型的列表),并且只从那个列表中添加丢失的电子邮件到新的列表中,然后我将这些电子邮件插入到我的数据库中......

使用示例:

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

“准备好的”列表是与旧列表进行比较的新列表..

现在我也想实现这个,只是针对不同的类和不同的标准规则:

- Items class

我有两个相同类型的列表(旧列表和新列表),其中包含以下数据:

- 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 列表中的项目的条件如下:

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

我可以使用一个 IEqualityComparer 来实现吗?有人可以帮帮我吗?

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

【问题讨论】:

  • 有什么问题-在第二种情况下,您只有多个相等条件。
  • @CodingYoshi 哈希码部分呢?
  • 一个简单的 LINQ 查询不能完成这项工作吗?喜欢:var thirdList = newItems.Where(x =&gt; !oldItems.Contains(x)).ToList()?如果一个简单的包含条件还不够,您可以使用Any 函数。
  • @TeodorKurtev 好点,但它不符合 quantiy 属性不相等的标准:D
  • @CodingYoshi 我已经更新了我的问题,这是正确的方法吗?

标签: c# asp.net asp.net-mvc c#-4.0 asp.net-mvc-5


【解决方案1】:

我编写了一个扩展方法,可以为您执行所需的行为。

如果新项目不在旧项目列表中,它将被添加到结果列表中。如果项目被修改,它也会被添加到结果列表中。

代码:

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

【讨论】:

  • 它说 CustomComparer.Instance 不存在?
  • 我只是使用了您在最初问题中的 CustomComparer 类来进行演示。您必须编写一个实现 IEqualityComparer 的类并将其传入。它可以是单例或新的,这取决于您,但这只是一个示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多