Linq Query, Please Help!

Hello Experts,

I have a datatable as below. I want to group by using Col3 & Col4 and take sum of Col2.
And display output as, specific column as shown below.

Input Datatable:

Col1 – Col2 – Col3 – Col4
A – 11 – AA – ABC
B – 12 – RT – EFG
C – 13 – HL – XYZ
D – 27 – AA – ABC
E – 17 – AA – TUV[Robot shld not sum this, because of col4 mismatch]
F – 15 – RT – EFG
G – 24 – HL – XYZ

OutPut required:

Col1 – Col2 - Col3
AA – 38 -ABC
AA - 17 - TUV
RT – 27-EFG
HL – 37-XYZ

Please help, @ppr @NIVED_NAMBIAR

@kantheshm

give a try on following

prepare an empty datatable with build datatable reflecting the target datacolumn strucutre:
Col1 – Col2 - Col3 Variable: dtResult

then use an assign activity
left side: dtResult
right side:
(From d in dtData.AsEnumerable
Group d by k1=d(“Col3”).toString.Trim,k2=d(“Col4”).toString.Trim into grp=Group
Let s = grp.Sum(Function (x) CInt(x(“Col2”).toString.Trim))
Ler ra = new Object(){k1,s,k2}
Select dtResult.Rows.Add(ra)).CopyToDataTable

Also have a look here:

2 Likes

Hi @ppr ,

It worked!!
Thanks for your help…

Last, one small query…

I have a 1 linq expression, suggested by you in other topic. In this, i want some columns from DT1 & some colums to DT2. In select i tried multiple ways but ended in vain.

Can you please help me with this!!

(From DT1 In dt1.AsEnumerable()
From DT2 In dt2.AsEnumerable()
Where DT1(14).ToString().contains(DT2(1).ToString())
Select DT2).ToArray().CopyToDatatable

Please help @ppr @NIVED_NAMBIAR

Hi @kantheshm
can u give an example for this ?

Hi @NIVED_NAMBIAR ,

Thank you for helping me…

Dt1
Col1 – Col2 – Col3 – Col4
A – 11 – AA – ABC
B – 12 – RT – EFG
C – 13 – HL – XYZ
D – 27 – AA – ABC
F – 15 – RT – EFG
G – 24 – HL – XYZ

Dt2
Col1 – Col2 – Col3 – Col4
A – 11 – AA – KLM
B – 12 – RT – NOP
C – 13 – HL – QRS
D – 27 – AA – WXC
F – 15 – RT – ASD
G – 24 – HL – MKL

output

Col1 – Col2 – Col3 – Col4-Col5
A – 11 – AA – ABC-KLM
B – 12 – RT – EFG-NOP
C – 13 – HL – XYZ-QRS
D – 27 – AA – ABC-WXC
F – 15 – RT – EFG-ASD
G – 24 – HL – XYZ-MKL

I am selecting data from 2 datatable, DT1 & DT2 after join.

Can you please help me on this…

Kindly note: From, From is working like a Cartesian Product. We would suggest to open a new topic for this case/ question as it is new one compared to the GroupBy request.

If we keep the topics only related to one case we can support researchers by fast identifiying case and solution. With marking the solving post as solution another feedback is given and others can benefit from it.

2 Likes

Got it @ppr

Can you please help me on this thread…

Hi @ppr ,

I got the expression.Thank you…

Struck on the same expression, that is, if we need col4 in dtresult, how can i achieve this!!

Looking forward for you response!!

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