67

そのため、.NET 3.0/3.5 では、LINQ で提供されるすべての優れた関数のおかげで、データのクエリ、並べ替え、および操作を行うための新しい方法が多数提供されます。場合によっては、比較演算子が組み込まれていないユーザー定義型を比較す​​る必要があります。多くの場合、比較は非常に単純です -- foo1.key ?= foo2.key のようなものです。型の新しい IEqualityComparer を作成するのではなく、匿名デリゲート/ラムダ関数を使用してインラインで比較を指定することはできますか? 何かのようなもの:

var f1 = ...,
    f2 = ...;
var f3 = f1.Except(
           f2, new IEqualityComparer(
             (Foo a, Foo b) => a.key.CompareTo(b.key)
           ) );

上記は実際には機能しないと確信しています。リンゴとリンゴを比較する方法をプログラムに伝えるためだけに、クラス全体として何かを「重い」ものにする必要はありません。

4

9 に答える 9

74

私のMiscUtilライブラリには、プロジェクション デリゲートから IComparer<T> を構築するための ProjectionComparer が含まれています。ProjectionEqualityComparer を作成して同じことを行うのに 10 分かかります。

編集: ProjectionEqualityComparer のコードは次のとおりです。

using System;
using System.Collections.Generic;

/// <summary>
/// Non-generic class to produce instances of the generic class,
/// optionally using type inference.
/// </summary>
public static class ProjectionEqualityComparer
{
    /// <summary>
    /// Creates an instance of ProjectionEqualityComparer using the specified projection.
    /// </summary>
    /// <typeparam name="TSource">Type parameter for the elements to be compared</typeparam>
    /// <typeparam name="TKey">Type parameter for the keys to be compared,
    /// after being projected from the elements</typeparam>
    /// <param name="projection">Projection to use when determining the key of an element</param>
    /// <returns>A comparer which will compare elements by projecting 
    /// each element to its key, and comparing keys</returns>
    public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey>(Func<TSource, TKey> projection)
    {
        return new ProjectionEqualityComparer<TSource, TKey>(projection);
    }

    /// <summary>
    /// Creates an instance of ProjectionEqualityComparer using the specified projection.
    /// The ignored parameter is solely present to aid type inference.
    /// </summary>
    /// <typeparam name="TSource">Type parameter for the elements to be compared</typeparam>
    /// <typeparam name="TKey">Type parameter for the keys to be compared,
    /// after being projected from the elements</typeparam>
    /// <param name="ignored">Value is ignored - type may be used by type inference</param>
    /// <param name="projection">Projection to use when determining the key of an element</param>
    /// <returns>A comparer which will compare elements by projecting
    /// each element to its key, and comparing keys</returns>
    public static ProjectionEqualityComparer<TSource, TKey> Create<TSource, TKey>
        (TSource ignored,
         Func<TSource, TKey> projection)
    {
        return new ProjectionEqualityComparer<TSource, TKey>(projection);
    }

}

/// <summary>
/// Class generic in the source only to produce instances of the 
/// doubly generic class, optionally using type inference.
/// </summary>
public static class ProjectionEqualityComparer<TSource>
{
    /// <summary>
    /// Creates an instance of ProjectionEqualityComparer using the specified projection.
    /// </summary>
    /// <typeparam name="TKey">Type parameter for the keys to be compared,
    /// after being projected from the elements</typeparam>
    /// <param name="projection">Projection to use when determining the key of an element</param>
    /// <returns>A comparer which will compare elements by projecting each element to its key,
    /// and comparing keys</returns>        
    public static ProjectionEqualityComparer<TSource, TKey> Create<TKey>(Func<TSource, TKey> projection)
    {
        return new ProjectionEqualityComparer<TSource, TKey>(projection);
    }
}

/// <summary>
/// Comparer which projects each element of the comparison to a key, and then compares
/// those keys using the specified (or default) comparer for the key type.
/// </summary>
/// <typeparam name="TSource">Type of elements which this comparer 
/// will be asked to compare</typeparam>
/// <typeparam name="TKey">Type of the key projected
/// from the element</typeparam>
public class ProjectionEqualityComparer<TSource, TKey> : IEqualityComparer<TSource>
{
    readonly Func<TSource, TKey> projection;
    readonly IEqualityComparer<TKey> comparer;

    /// <summary>
    /// Creates a new instance using the specified projection, which must not be null.
    /// The default comparer for the projected type is used.
    /// </summary>
    /// <param name="projection">Projection to use during comparisons</param>
    public ProjectionEqualityComparer(Func<TSource, TKey> projection)
        : this(projection, null)
    {
    }

    /// <summary>
    /// Creates a new instance using the specified projection, which must not be null.
    /// </summary>
    /// <param name="projection">Projection to use during comparisons</param>
    /// <param name="comparer">The comparer to use on the keys. May be null, in
    /// which case the default comparer will be used.</param>
    public ProjectionEqualityComparer(Func<TSource, TKey> projection, IEqualityComparer<TKey> comparer)
    {
        if (projection == null)
        {
            throw new ArgumentNullException("projection");
        }
        this.comparer = comparer ?? EqualityComparer<TKey>.Default;
        this.projection = projection;
    }

    /// <summary>
    /// Compares the two specified values for equality by applying the projection
    /// to each value and then using the equality comparer on the resulting keys. Null
    /// references are never passed to the projection.
    /// </summary>
    public bool Equals(TSource x, TSource y)
    {
        if (x == null && y == null)
        {
            return true;
        }
        if (x == null || y == null)
        {
            return false;
        }
        return comparer.Equals(projection(x), projection(y));
    }

    /// <summary>
    /// Produces a hash code for the given value by projecting it and
    /// then asking the equality comparer to find the hash code of
    /// the resulting key.
    /// </summary>
    public int GetHashCode(TSource obj)
    {
        if (obj == null)
        {
            throw new ArgumentNullException("obj");
        }
        return comparer.GetHashCode(projection(obj));
    }
}

使用例は次のとおりです。

var f3 = f1.Except(f2, ProjectionEqualityComparer<Foo>.Create(a => a.key));
于 2008-10-09T16:43:50.827 に答える
22

ここにあなたが望むことをするべきである単純なヘルパークラスがあります

public class EqualityComparer<T> : IEqualityComparer<T>
{
    public EqualityComparer(Func<T, T, bool> cmp)
    {
        this.cmp = cmp;
    }
    public bool Equals(T x, T y)
    {
        return cmp(x, y);
    }

    public int GetHashCode(T obj)
    {
        return obj.GetHashCode();
    }

    public Func<T, T, bool> cmp { get; set; }
}

あなたはそれをこのように使うことができます:

processed.Union(suburbs, new EqualityComparer<Suburb>((s1, s2)
    => s1.SuburbId == s2.SuburbId));
于 2012-05-23T12:46:04.883 に答える
7

次のようなものではないのはなぜですか:

    public class Comparer<T> : IEqualityComparer<T>
    {
        private readonly Func<T, T, bool> _equalityComparer;

        public Comparer(Func<T, T, bool> equalityComparer)
        {
            _equalityComparer = equalityComparer;
        }

        public bool Equals(T first, T second)
        {
            return _equalityComparer(first, second);
        }

        public int GetHashCode(T value)
        {
            return value.GetHashCode();
        }
    }

そして、たとえば次のようなことを行うことができます(たとえば in の場合IntersectIEnumerable<T>

list.Intersect(otherList, new Comparer<T>( (x, y) => x.Property == y.Property));

このComparerクラスは、ユーティリティ プロジェクトに配置して、必要な場所で使用できます。

Sam Saffron の回答 (これと非常によく似ています) しか表示されません。

于 2015-04-01T14:24:19.310 に答える
1

小さなセットの場合、次のことができます。

f3 = f1.Where(x1 => f2.All(x2 => x2.key != x1.key));

大きなセットの場合、次のような検索でより効率的なものが必要になります。

var tmp = new HashSet<string>(f2.Select(f => f.key));
f3 = f1.Where(f => tmp.Add(f.key));

ただし、ここでは、Typeofキーを実装する必要がありますIEqualityComparer(上記では、 であると想定していましたstring)。したがって、これは、この状況でラムダを使用することについてのあなたの質問には実際には答えませんが、使用するコードはいくつかの回答よりも少なくなります。

オプティマイザーに頼って、2 番目のソリューションを次のように短縮できます。

f3 = f1.Where(x1 => (new HashSet<string>(f2.Select(x2 => x2.key))).Add(x1.key));

ただし、同じ速度で実行されるかどうかを確認するテストは実行していません。そして、その1つのライナーは、維持するには賢すぎるかもしれません.

于 2015-10-23T13:25:12.733 に答える
1

他の回答に基づいて構築する汎用比較子の作成は、私が最も気に入ったものでした。しかし、Linq Enumerable.Union( msdn .Net 参照) で問題が発生しました。これは、Equals オーバーライドを考慮せずに GetHashCode を直接使用することでした。

Comparer を次のように実装する必要がありました。

public class Comparer<T> : IEqualityComparer<T>
{
    private readonly Func<T, int> _hashFunction;

    public Comparer(Func<T, int> hashFunction)
    {
        _hashFunction = hashFunction;
    }

    public bool Equals(T first, T second)
    {
        return _hashFunction(first) == _hashFunction(second);
    }

    public int GetHashCode(T value)
    {
        return _hashFunction(value);
    }
}

次のように使用します。

list.Union(otherList, new Comparer<T>( x => x.StringValue.GetHashCode()));

比較される情報は値にマップされるため、比較によって誤検出が生じる可能性があることに注意してくださいint

于 2019-06-12T11:35:26.140 に答える