I have a datatable that contains various columns. Now, some of the columns are Invoice Number, Material, Price etc etc.
I am facing an problem while writing a linq, the condition being if Invoice Number and Material both are same then i need to avg the price and write in all the places. Like if One invoice number is present in 3 rows and all three rows are having same material then we need to avg the price in the 3 rows and fill in the price column of all the three rows.
In general a Group By Case which we can handle as described here:
We would recommend to start with a hybrid approach
Groups | DataType: List(of (ListOf DataRow)) =
(From d in dtData.AsEnumerable
Group d by k1=d("InvoiceNo.").toString.Trim, k2=d("MATERIAL CODE.").toString.Trim into grp = Group
Select x=grp.ToList).toList
outer List = Groups, inner List = Group Members
For Each Activity (not for each row activity) | grp = Groups | TypeArgument: List(Of DataRow)
hey @ppr , how is this happening? I mean, we are modifying the dt_?list but how come the datatable is getting modified on its own? and what is the role of F2 in strAVG = grp.Average(Function (x) CDbl(x(“Price”).toString.Trim)).toString(“F2”)
each datarow is belonging to its datatable and is referencing it via the Navigation Property: Table. So when working with the datarow it is also relected within the datatable
Acting like a local loop variable. Reflecting the looped group member datarow.