ソースコード
using System;
using System.Collections.Generic;
using System.Linq;
public static class IEnumerableExtensions
{
private sealed class CommonSelector<T, TKey> : IEqualityComparer<T>
{
private Func<T, TKey> m_selector;
public CommonSelector( Func<T, TKey> selector )
{
m_selector = selector;
}
public bool Equals( T x, T y )
{
return m_selector( x ).Equals( m_selector( y ) );
}
public int GetHashCode( T obj )
{
return m_selector( obj ).GetHashCode();
}
}
public static bool Contains<T, TKey>(
this IEnumerable<T> source,
T value,
Func<T, TKey> selector
)
{
return source.Contains(
value,
new CommonSelector<T, TKey>( selector )
);
}
}
使い方
public class Character
{
public int m_id ;
public string m_name;
}
...
var list = new List<Character>();
var value = new Character();
list.Contains( value, c => c.m_name );
参考サイト様
neue cc - LinqとIEqualityComparerへの疑問