0

Let's say I want to find out which eprt_id appears twice. (Which in this case is: 20463)

Eprt_id: 21487 Perceel: Test
               subject: Apples
 
Eprt_id: 21696 Perceel: Test2
               subject: Oranges

Eprt_id: 21822 Perceel: Test3
               subject: Pears

Eprt_id: 23485 Perceel: Test4
               subject: Lemons

Eprt_id: 24238 Perceel: Test55
               subject: Watermelons
               subject: Oranges

Eprt_id: 20463 Perceel: Test56
               subject: Apples
               subject: Pears

Eprt_id: 20463 Perceel: Test57
               subject: Apples
               subject: Pears

This list is obviously a group on Eprt_id and Perceel.

But how can I write a decent linq statement that gives me the result (in this case) 20463 occurs more than once?

I got this list as follows:

var nextGroupedWorklist = from r in SubtractBSNlessFromWerklijst
                where r.Eprt_id.HasValue &&
                    !string.IsNullOrEmpty(r.Subject_id) &&
                    !string.IsNullOrEmpty(r.Perceel_id) &&
                    !string.IsNullOrEmpty(r.Hermes_dossier_nr) &&
                    !string.IsNullOrEmpty(r.Gogis_perceel_nr)
                group r by new { r.Eprt_id, r.Gogis_perceel_nr };

I thought a simple count would do. But it obviously returns a count of the elements of the group.

I found a way to get 20463. But is this the best way?

 var solution = from r in nextGroupedWorklist
    group r by new { r.Key.Eprt_id }
    into grp
    where grp.Count() > 1
    select grp;

Secondly is there a clever way to check if both subjects are the same? (As they are in the example) Both 20463 eprt_id's contain the subject: Apples and Pears. This an example list (as you can see 20463 each has two subjects that are the same in the 2 different Perceel columns) So that would be what I want to check. If they are both the same. I may proceed processing 20463. Otherwise not.:

 Eprt_id: 20229 Perceel: WPK02L1072G0 subject: NL.KAD.Persoon.255545276 
 Eprt_id: 20267 Perceel: WPK02L1051G0 subject: NL.KAD.Persoon.255545276 
 Eprt_id: 20463 Perceel: ASD31AL2317G0 subject: NL.KAD.Persoon.170412796
 Eprt_id: 20463 Perceel: ASD31AL2317G0 subject: NL.KAD.Persoon.455686300
 Eprt_id: 20463 Perceel: ASD31AL3076G0 subject: NL.KAD.Persoon.170412796
 Eprt_id: 20463 Perceel: ASD31AL3076G0 subject: NL.KAD.Persoon.455686300
 Eprt_id: 20524 Perceel: ASD31AL952G0 subject: NL.KAD.Persoon.170348494 
 Eprt_id: 20524 Perceel: ASD31AL952G0 subject: NL.KAD.Persoon.171028089 
 Eprt_id: 20939 Perceel: WPK02L260G0 subject: NL.KAD.Persoon.171510661 
 Eprt_id: 20939 Perceel: WPK02L260G0 subject: NL.KAD.Persoon.171510669 
 Eprt_id: 20944 Perceel: WPK02L6672G0 subject: NL.KAD.Persoon.171510661
 Eprt_id: 20944 Perceel: WPK02L6672G0 subject: NL.KAD.Persoon.171510669 
 Eprt_id: 21487 Perceel: WPK02L2503G0 subject: NL.KAD.Persoon.170459916 
 Eprt_id: 21696 Perceel: ASD02A7343A61 subject: NL.KAD.Persoon.171440573
 Eprt_id: 21822 Perceel: WPK02L1602G0 subject: NL.KAD.Persoon.459669099 
  • 1
    https://stackoverflow.com/questions/2078736/linq-with-group-by-having-count – greenjaed Dec 22 '20 at 23:44
  • Please post the full type definition of `SubtractBSNlessFromWerklijst`. How is `subject` declared? – Dai Dec 23 '20 at 00:34
  • Do you need the whole grouping as well as knowing if part of the grouping repeats? – Caius Jard Dec 23 '20 at 08:09
  • Yes indeed. So first I'm going to get the grouping withouth double for futher processing. And find out which are double so I can do some checks. To see if the Eprt_id is allowed to be processed. – Triodefreak Dec 23 '20 at 14:48

1 Answers1

2
List<(int eprt, int count)> duplicates = SubtractBSNlessFromWerklijst
    .Where( r => r.Eprt_id.HasValue )
    .GroupBy( r => r.Eprt_id.Value )
    .Where( grp => grp.Count() > 1 )
    .Select( grp => ( eprt: grp.Key, count: grp.Count() ) )
    .ToList();

This will give you this output:

[
    ( eprt: 20463, count: 2 )
]

Secondly is there a clever way to check if both subjects are the same?

We can't help you there because you haven't posted the definition of whatever type holds subject. Your example input data does not explain how the object-graph is structured.

Dai
  • 141,631
  • 28
  • 261
  • 374