Hi all,
I have been searching the forum but cant find any help.
I have a data table DT1 that contains approx 20 columns. I would like to do a group by, insert the result in a new data table DT2 and do a left join between DT1 and DT2. How can I accomplish this?
DT1:
Order ID, Serial No, Amount, Column x, Column z etc
DT2:
Select Order ID, Serial No, Sum(Amount) as Total
From DT1
Group by Order ID, Serial No
DT1:
Select * from DT1
left join DT2 on DT1.Order ID = DT2.Order ID and DT1.Serial No=DT2.Serial No
@cristian_ivanoff
DT2= (From p In dt1.AsEnumerable()
Group By x= New With { Key.a =p.Item(“OrderID”),Key.b=p.Item(“SerialNo”)}
Into Group
Select Group(0)).ToArray().CopyToDataTable()
Create a Create a List of strings
ListA=(From p In DT1.AsEnumerable()
Group By x= New With { Key.a =p.Item(“OrderID”),Key.b=p.Item(“SerialNo”)}
Into GroupA= Group
Select Convert.ToString(GroupA.Sum(Function(x) Convert.ToDouble(x.Item(“Amount”).ToString)))).ToList()
Now DT2=DT2.DefaultView.ToTable(False,“OrderID”,Serial NO")
Now Add one datacolumn to DT2 and Name it as Total
Next Run one Foreach loop for DT2
Inside for each use one assign activity
row(“Total”)=ListA(dt1.Rows.IndexOf(row)).ToString
AtLast use the below Query.
DT3=(From p In DT1.Select()
Group Join q In DT2.Select
On p(“OrderID”) Equals q(“OrderID”) and p(“Serial No”) Equals q(“Serial No”) Into abc=Group
From g In abc.DefaultIfEmpty()
Select p).ToArray.CopyToDataTable()
Use this one @cristian_ivanoff (From p In dt1.AsEnumerable() Group By x= New With { Key.a =p.Item("OrderID"),Key.b=p.Item("SerialNo")} Into Group Select Group(0)).ToArray().CopyToDataTable()
@cristian_ivanoff
Try this once
DT2= (From p In dt1.AsEnumerable()
Group p By x= New With { Key.a =p.Item(“OrderID”),Key.b=p.Item(“SerialNo”)}
Into Group
Select Group(0)).ToArray().CopyToDataTable()
@cristian_ivanoff
Try this it might help you
ListA=(From p In DT1.AsEnumerable()
Group By y= New With { Key.a =p.Item(“OrderID”),Key.b=p.Item(“SerialNo”)}
Into GroupA= Group
Select Convert.ToString(GroupA.Sum(Function(x) Convert.ToDouble(x.Item(“Amount”).ToString)))).ToList()
Even here also you have to use original datatable DT1, not the grouped one
may be a neat method,but please notice that:
DefaultView.ToTable has a bottleneck on record numbers which is be confirmed bad performs by statistics below:
ListA=(From p In DT1.AsEnumerable()
Group By x= New With { Key.a =p.Item(“OrderID”),Key.b=p.Item(“SerialNo”)}
Into GroupA= Group
Select Convert.ToString(GroupA.Sum(Function(x) Convert.ToDouble(x.Item(“Amount”).ToString)))).ToList()