Datatable Group

Good forums everyone,

I would like your help to make a datatable grouping. I opened a forum similar to this before and I had to make changes because there were different developments.

My sample Datatable file is as follows. In this table, I will make a calculation for the purchases made for the same material number. In the “Place of Purchase” column, I will calculate the difference between the last purchase starting with “43” and the last purchase starting with “42”. I need to pull and process the “43” and “42” data of the same material number from this table.

image

As a result, we should have 1 information starting with “43” and 1 information starting with “42” for each material number. According to this information, transactions will be made according to “Amount” and “Currency” information.

For example for material number 1003;

The most current domestic and international purchase information.

“4245 1003 11.10.2022 2500 TL” starting with “42”
“4348 1003 24.10.2023 EUR 6500” starting with “43”

Is there a Linq query so that I can have all the data like the data above?
Thank you in advance.

Maybe a start with grouping the data by Mat No and beginning Place can help

Assign activity
Groups | List(Of List(of Datarow)) =

(From d in dtData.AsEnumerable
Group d by k1=d("Place...").toString.Trim.Substring(0,2), k2=d("Material Number").toString.Trim into grp=Group
Select g=grp.ToList).toList

outer List: Groups, inner List: the group members

Now we can iterate and post process the Group Members e.g. within a for each /and nested for each

The handling of Date and Currency is not really clear, and maybe needs to get specified more sharp

Also give a try at the following:

Assign activity:
dtResult = dtData.Clone

Assign Activity
dtResult =

(From d in dtData.AsEnumerable
Group d by k1=d("Place...").toString.Trim.Substring(0,2), k2=d("Material Number").toString.Trim into grp=Group
Let asm = grp.Sum(Function (g) CDbl(g("Amount..").toString.Trim))
Let ra = new Object(){grp(0)(0),k2, grp(0)(2),grp(0)(3),asm }
Select r=dtResult.Rows.Add(ra)).CopyToDataTable

We recommend also to check and maybe adapt

  • Double conversion
  • First memeber retrievals
1 Like

Thank you very much. After editing a few things, your query worked. I really appreciate it very much.

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