有2个比较器:
public class EqualityComparer123 : IEqualityComparer
{
Func pred;
public EqualityComparer123(Func 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 : IEqualityComparer
{
Func pred;
public TEqualityComparer(Func 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();
ca.Add(new ClassA() { id = 1, name = "nima" });
ca.Add(new ClassA() { id = 2, name = "tama" });
var cb = new List();
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((x, y) => { return x.id == y.id; });
var query3 = ca.Join(cb, x => x, y => y, (x, y) => new { CA = x, CB = y }, Compare3);
其中query1能够查询出数据,而query3却查询不出数据,望解惑。。
1 回答
- 1 回答
- 0 关注
- 511 浏览
添加回答
举报
0/150
提交
取消