user3487843
user3487843

Reputation:

Linq query explained in plain english

I have a Linq query I'm not 100% sure of, can someone explain it in plain English what is happening

var duplicates = this.BedStays
                  .GroupBy(i => new { i.PatientClassificationCode, i.BedBandCode, i.BedLevelAddOnInd, i.ServiceCodeTypeCode, i.ServiceCode, i.DayRate })
                  .Where(g => g.Count() > 1)
                  .Select(g => g.Key).ToList();

Upvotes: 0

Views: 124

Answers (3)

Rob
Rob

Reputation: 27377

It will give you all duplicate rows which have the same

  • PatientClassificationCode
  • BedBandCode
  • BedLevelAddOnInd
  • ServiceCodeTypeCode
  • ServiceCode
  • DayRate

It does this by grouping rows together which have the same criteria, and then only displaying the groups with more than one row (that is, duplicates).

Upvotes: 2

Ranger
Ranger

Reputation: 1174

var duplicates = this.BedStays

...Assigning to a var called "duplicates" from a property in this class called "BedStays."

.GroupBy(

...Grouping by a set of specific traits...

i =>

Called a single BedStay "i" (could have called it instead "aBedStay" or "bedStay" but the author went with "i")...

 new { i.PatientClassificationCode, i.BedBandCode, i.BedLevelAddOnInd, i.ServiceCodeTypeCode, i.ServiceCode, i.DayRate })

...Creating a new object to sort by (First by the first member, then by the second, and so on and so forth)...

.Where(g => g.Count() > 1)

...Only picking the bedStay groupings where there's at least one matching member (I believe this is unneeded as all groupings, by definition, will only exist if from at least one grouping definition)...

.Select(g => g.Key).ToList();

...And finally we're only caring about the key, which is the grouping we defined when we new'd up the {i.Property, i.Property, i.Property} object. And we make it a list.

Upvotes: 1

Hari Prasad
Hari Prasad

Reputation: 16986

can someone explain it in plain English what is happening

Query is primarily listing all duplicates in your BedStays collection, by grouping on PatientClassificationCode, BedBandCode, BedLevelAddOnInd, ServiceCodeTypeCode, ServiceCode, DayRate fields.

Upvotes: 0

Related Questions