我有兩個列表,我需要找到第二個列表中缺少的項目,但我只能將它們與布爾函數進行比較。什麼是最簡潔的方式來做一個沒有等號的外連接?
class A
{
internal bool Matching(A a)
{
return true;
}
}
class OuterMatch
{
List<A> List1 = new List<A>();
List<A> List2 = new List<A>();
void BasicOuterJoin()
{
// textbook example of an outer join, but it does not use my Matching function
var missingFrom2 = from one in List1
join two in List2
on one equals two into matching
from match in matching.DefaultIfEmpty()
where match == null
select one;
}
void Matching()
{
// simple use of the matching function, but this is an inner join.
var matching = from one in List1
from two in List2
where one.Matching(two)
select one;
}
void MissingBasedOnMatching()
{
// a reasonable substitute for what I'm after
var missingFrom2 = from one in List1
where (from two in List2
where two.Matching(one)
select two)
.Count() == 0
select one;
}
MissingBasedOnMatching
給了我正確的結果,但它看起來並不明顯外連接一樣BasicOuterJoin
是。有沒有更清楚的方法來做到這一點?
有羣組加入的一種形式,需要一個比較運營商,但我不清楚是否有使用它來使外部聯接的方式。
到達那裏......我無法覆蓋Foo,但我可以編寫一個新的FooComarer,並且它比LINQHelper解決方案的開銷稍小。 –