我有两个列表,我需要在第一个列表中找到第二个列表中缺少的项目,但我只能将它们与布尔函数进行比较。
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
。有没有更清晰的方法来做到这一点?
有一种采用比较运算符的 GroupJoin 形式,但我不清楚是否有办法使用它来进行外部联接。