Search for records with the same and different fields

From the list of objects containing the following information (id, name, address, code):

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

I want to get strings with the same name and address, but with a different code. In this example:

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

I went about it with Linq, learning about it for the first time. I managed to find duplicates:

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

But I can’t find a way to make sure that I only get records with different codes. I would also get the last line, but since it has the same code, I don't want this. Is there any way to do this with Linq? Or any other way in C #?

Thanks!

+4
source share
1 answer

You can use Distinctto get different values

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;
+5
source

Source: https://habr.com/ru/post/1621737/


All Articles