Linq grouping check if empty or null

Hello all,

thanks to the support in the forum I got a way to group values in a table, it works.

What is new is that the value to be grouped may not be in the table. How can I catch this problem?

That is, I would like to check if the value to be grouped is in the table at all before grouping it.

(From d In DT_from_Report.AsEnumerable
Group d By k=d(str_OrderNr).toString.Trim Into grp=Group
Select g=grp.CopyToDataTable).ToList

Hi @NHoe

You can try with this expression

(From d In DtBuild.AsEnumerable
Group d By k=d("Column name").toString.Trim Into grp = Group
Let ra = New Object(){k}
Select r = DtClone.Rows.Add(ra)).CopyToDataTable

Regards
Gokul

Hi @Gokul001

i get only one row, not all

(From d In DT_from_Report.AsEnumerable
Group d By k=d(“Number”).toString.Trim = OrderNr Into grp=Group
Let ra =New Object(){grp.First()(0),grp.First()(1),grp.First()(2),grp.First()(3),grp.First()(4)}
Select r=DT_clone.Rows.Add(ra)).CopyToDataTable

Hi @NHoe

Can you share the sample input and expected output

Hi @Gokul001

i want group the Number and after them i want all rows to this number

Kundennummer Artikelnummer LSNummer LSposition Anzahl Number
523778 40-49-9844 702104086194 01 1 8107063
523778 40-46-8251 702104086194 02 1 8107063
523778 40-47-9314 702201615359 01 1 2861873
523778 40-28-4467 702201615359 02 1 2861873
523778 40-53-1906 702201686085 01 1 2861873
523778 40-45-4995 702203376824 03 1 3394732
523778 40-54-2993 702203376824 01 1 3394732
523778 40-28-4473 702203376824 02 1 3394732

Hi @NHoe

How about this expression

(From d In DtBuild.AsEnumerable
Group d By k=d("Number").toString.Trim Into grp = Group
Let ra = New Object(){grp.First()(0),grp.First()(1),grp.First()(2),grp.First()(3),grp.First()(4),k}
Select r = DtClone.Rows.Add(ra)).CopyToDataTable

Check out this Sample XAML file

GroupByLinq.xaml (18.7 KB)

Regards
Gokul

Hi @Gokul001

it doesnt work, i get only the First row from the “Number”

for example = i start with the Group d By k=d(“Number”).toString.Trim = OrderNr Into grp=Group “8107063” and i want all rows from thet DataTable

Kundennummer Artikelnummer LSNummer LSposition Anzahl Number
523778 40-49-9844 702104086194 01 1 8107063
523778 40-46-8251 702104086194 02 1 8107063

Can you share the output for this? @NHoe

Have you tried with Filter data table activity @NHoe

maybe you can elaborate more on this. GroupBy is working dynamic, so it will only use the present values.

That works fine, thank you!

1 Like

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