Excel linq group rows based on two rows in a datable and also if second column is distinct

Hello Guys,

I am trying to group a datatable based on two columns a and b.
for example
col a,col b
a,b
a,b
c,d
e,d
c,d
f,g

so here I want output is

col a,col b
a,b
a,b
f,g
but I am getting output as

col a,col b
a,b
a,b
c,d
c,d

which shouldn’t be the output since b column has same value “d” but distinct values in “d” value group
linq query I am using is

(From d In dt_Master.AsEnumerable Where (Not String.IsNullOrEmpty(“a”).ToString))
Group d By k1= d(“a”).toString.trim,k2 =d(“b”).ToString.Trim.Contains(row(“unqiue value list in b column”).ToString) Into grp=Group
Let ra = New Object(){k1,k2}
Where grp.Count > 0
Where grp.Select(Function (x) x(“Ship-to party’s purchase order no.”).toString.Trim).Distinct().Count <= 1
Select dt_Result.Rows.Add(ra)).CopyToDatable

so conclusion is I want dt whose b column is same in a group and also a column’s all value should be same . if only one row is available than it its fine but if more than one row is present a column values should be thought the group

when grouping the data on col b we can check if the group members a col values are dictinct

(From d In dtData.AsEnumerable
Group d By k=d(1).toString.Trim Into grp=Group
Let chk = (grp.Select(Function (x) x(0).ToString.Trim).Distinct().Count = 1)
Where chk
Select g=grp).SelectMany(Function (g) g).CopyToDataTable

in case of we have to expect an empty result as well we can handle defensive by:
grafik

grafik

Find starter help here:
BuildGroups_2Col_1ColDistinctCheck.xaml (8.7 KB)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.