如何从ID不匹配的两个列表中选择项目

时间:2012-08-01 02:39:43

标签: c# linq

好的,所以我有两个不同类型的项目列表。

var whales = new List<Whale>();

var crabs = new List<Crab>();

所以他们都有id属性。因此,保存列表的对象的ID为:

鲸鱼:1,3,4,5 螃蟹:1,2,3,4

好的,我有一个问题:

var matchedPairs = from c in crabs
                   from w in whales
                   where c.Id = w.Id
                   select new { crab = c, whale = w };

这样可以很好地获得比赛。在我遇到麻烦的地方,我想得到一份没有匹配鲸鱼的螃蟹清单,即。 Crab Id = 2.然后我想得到没有匹配螃蟹的鲸鱼,即鲸鱼Id = 5。

谁能告诉我如何编写这些查询?

7 个答案:

答案 0 :(得分:8)

如果你只想选择螃蟹。

var result = crabs.Where(c => whales.All(w => w.Id != c.Id));

答案 1 :(得分:4)

也许是这样的:

var unmatchedCrabs = from c in crabs
                     where !whales.Any(w => w.Id == c.Id)
                     select c;

答案 2 :(得分:2)

您需要outer join

var crabsWithoutWhales = from c in crabs
                         join w1 in whales on c.Id equals w1.Id into ws
                         from w2 in ws.DefaultIfEmpty()
                         where w2 == null
                         select c;

答案 3 :(得分:1)

您可以使用Union operation中的set operations

要使用它,您必须覆盖默认Equality comparer and GetHashCode method。一旦你有了那些,那么你可以做这样的事情:

var matchedPair = crabs.Union(whales);

在你的情况下,你应该有一个基类;例如具有平等比较的动物。另一种选择是实施IEqualityComparer<>

答案 4 :(得分:0)

var result = crabs.SelectMany(c => whales, (c, w) => new { c, w })
                              .Where(@t => whales.All(x => x.Id != t.c.Id) && crabs.All(x => x.Id != t.w.Id))
                              .Select(@t => new {crab = @t.c, whale = @t.w});

答案 5 :(得分:0)

这是我能想到的最好看的LINQ来做你需要的:

var whalesOnly =
    from w in whales
    join c in crabs on w.Id equals c.Id into gcs
    where !gcs.Any()
    select w;

var crabsOnly =
    from c in crabs
    join w in whales on c.Id equals w.Id into gws
    where !gws.Any()
    select c;

这些如何找你?

顺便说一下,你可以像这样更好地加入查询:

var whalesAndCrabs =
    from whale in whales
    join crab in crabs on whale.Id equals crab.Id
    select new { crab, whale };

答案 6 :(得分:0)

你需要通过GroupJoin方法进行两次左连接:

var result1 = whales.GroupJoin(crabs, w => w.ID, c => c.ID, (w,cs) => new {WhaleID = w.ID, Matches = cs});
var result2 = crabs.GroupJoin(whales, c => c.ID, w => w.ID, (c, ws) => new {CrabID = c.ID, Matches = ws});

然后,按照您想要的结果过滤结果。

相关问题