LINQ - how to count items in each group and join them

Just invest some learning into getting the main concept of grouping data:

then decide if you want to go with LINQ / Non-LINQ approach

Whenever we use LINQ along with a datatable reconstruction approach we depend on the data table column structures

Assign Activity:
dtResult = dtData.Clone

Assign Activity

(From d In dtData.AsEnumerable
Group d By k=d("Dpt").toString.Trim Into grp=Group
Let grps2=  grp.GroupBy(Function (x) x("Name").toString.Trim)
Let sb = grps2.Select(Function (g) If(g.Count = 1, g.Key, $"{g.Key} ({g.Count()})"))
Let ra = New Object(){k, String.Join(", ", sb)}
Select r=dtResult.Rows.Add(ra)).CopyToDataTable
1 Like