查找具有相似和不同字段的记录

Finding records with similar and distinct fields

提问人:coconut 提问时间:12/26/2015 更新时间:12/26/2015 访问量:39

问:

从包含以下信息(ID、名称、地址、代码)的对象列表中:

1,Andrea,15th St,123
2,Sarah,15th St,124
3,Andrea,15th St,134
4,Andrea,16th St,124

我想检索具有相同名称和地址但代码不同的行。在此示例中:

1,Andrea,15th St,123
3,Andrea,15th St,134

我和 Linq 一起研究过它,这是我第一次了解它。我能够找到重复项:

var sameEmail =
        from l in list
        group l by new { l.name, l.address} into na
        where na.Count() > 1
        select na.Key;

但是我找不到一种方法来确保我只获得具有不同代码的记录。有了它,我也会得到最后一行,但由于它具有相同的代码,我不想要它。有没有办法用 Linq 做到这一点?还是 C# 中的任何其他方式?

谢谢!

C# LINQ

评论


答:

5赞 Olivier Jacot-Descombes 12/26/2015 #1

您可以使用 来获取不同的值Distinct

var sameEmail =
    from l in list
    group l by new { l.name, l.address} into na
    where na.Select(a => a.code).Distinct().Count() > 1
    select na.Key;

评论

0赞 coconut 12/26/2015
呸。现在太明显了。多谢!