You may try:
var result = listOne.Join(listTwo,
(one) => one,
(two) => two,
(one, two) => one,
new MyFooComparer());
Where MyFooComparer
could look like:
class MyFooComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo x, Foo y)
{
return x.Id == y.Id && x.someKey != y.someKey;
}
public int GetHashCode(Foo obj)
{
return obj.Id.GetHashCode();
}
}
[UPDATE]
I was curious about the performance of Intersect
vs. Join
so I made a small performance comparison between my solution and @p.s.w.g.'s (listOne
and listTwo
have 10 items each):
var comparer = new MyFooComparer();
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 100000; i++)
{
var result1 = listOne.Intersect(listTwo, comparer).ToList();
}
Console.WriteLine("Intersect: {0}",sw.Elapsed);
sw.Restart();
for (int i = 0; i < 100000; i++)
{
var result = listOne.Join(listTwo,
(one) => one,
(two) => two,
(one, two) => one,
comparer);
}
Console.WriteLine("Join: {0}", sw.Elapsed);
The output:
Intersect: 00:00:00.1441810
Join: 00:00:00.0037063