日期:2014-05-20 浏览次数:20854 次
public class EqualityComparer123 : IEqualityComparer<ClassA>
{
Func<ClassA, ClassA, bool> pred;
public EqualityComparer123(Func<ClassA, ClassA, bool> pred)
{
this.pred = pred;
}
public bool Equals(ClassA x, ClassA y)
{
//return x.id == y.id;
return pred(x, y);
}
public int GetHashCode(ClassA obj)
{
return base.GetHashCode();
}
}
public class TEqualityComparer<T> : IEqualityComparer<T>
{
Func<T, T, bool> pred;
public TEqualityComparer(Func<T, T, bool> pred)
{
this.pred = pred;
}
public bool Equals(T x, T y)
{
return pred(x, y);
}
public int GetHashCode(T obj)
{
return obj.GetHashCode();
}
}
public class ClassA
{
public int id { set; get; }
public string name { set; get; }
}
var ca = new List<ClassA>();
ca.Add(new ClassA() { id = 1, name = "nima" });
ca.Add(new ClassA() { id = 2, name = "tama" });
var cb = new List<ClassA>();
cb.Add(new ClassA() { id = 1, name = "nima2" });
cb.Add(new ClassA() { id = 3, name = "tama2" });
var Compare = new EqualityComparer123((x, y) => { return x.id == y.id; });
var query = ca.Join(cb, x => x, y => y, (x, y) => new { CA = x, CB = y }, Compare);
var Compare3 = new TEqualityComparer<ClassA>((x, y) => { return x.id == y.id; });
var query3 = ca.Join(cb, x => x, y => y, (x, y) => new { CA = x, CB = y }, Compare3);