I need explaination of this linq query

for group by have a look here:

and here (HowTo is work in progress / in review)

Statement is interpretated as:

  • find from all data grouped on rcp_no the first group member if the group size is greater then 1

(From d In TestDt
Group d By k=d(“rcp_no”).toString.Trim Into grp=Group
Where grp.Count >1
Select grp.toList).SelectMany(Function (x) x).CopyToDatatable.AsEnumerable.GroupBy(Function(x) x(“rcp_no”).ToString.Trim).Select(Function(y) y.First).CopyToDataTable()

Line1: iterate over testDT (in case of a datatable using additional AsEnumerable is highly recommendend)

Line2: group the data by the rcp_no value, reference the group with grp
Line3: filter out all groups with a group count < 1 (is detecting duplicates related to rcp_no)
Line4: select from the group (a collection) the individual items (Select many work)

last part is about adding the the first member form the group to a datatable and has a potential to get rewritten for a more clear expression

maybe in that form:
(From d In TestDt
Group d By k=d(“rcp_no”).toString.Trim Into grp=Group
Where grp.Count >1
Select grp.toList.First()).CopyToDatatable

3 Likes