Split table base on two column with LinQ

Hi Everyone.

I have DataTable as attached file.

I want to split into another table base on “item” & “Number” Cols

The Condition: The values at “Number” Col are < 0 and The values at “item” are same.

I have used linQ code as below but it printed result not correct.

Where is mistake in my code?

Thanks in advance!

(From d In table.AsEnumerable
Group d By k=d(“item”).toString.Trim(), k1 = Convert.ToDouble(d(“Number”).toString.Trim) < 0 Into grp=Group
Where grp.Count > 1
Select grp.ToList).SelectMany(Function (x) x).CopyToDataTable

Book1.xlsx (8.3 KB)

image

Hi Bro @Yoichi :).

You have any suggest for this case?

Thanks you!

Hi,

Can you elaborate your expected output? Is it single datatable?

Regards,

1 Like

Hi Bro

The output will be as below
image

Thanks in advance!

Hi,

What if there are negative value at “Number” column in No.6 and 8 row? Do you want to output to still single datatable or two datatables for each item?

Regards,

1 Like

Hi Bro.

I only want to split into single table… i will write a new table into another sheet.

Thanks you!

Hi,

Can you try the following? (Sorry, it’s LINQ method syntax because i’m familiar with it)

dt = dt.AsEnumerable.Where(Function(r)Int32.Parse(r("Number").ToString)<0).GroupBy(Function(r) r("item").ToString).Where(Function(g) g.Count>1).SelectMany(Function(g) g).CopyToDataTable

Regards,

2 Likes

Thanks you bro!

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