LINQ - Group By columns and Sum

Hi there!!!,

below is my actual excel columns :slight_smile:

table1

i want to apply this logic using LINQ - " Select category,sum(Amount) from tab_name group by category" — will get the output below ::::

table2

I tried using LINQ…but i am not getting right query to solve this…pls help me on this…

@fayaz You will need to update the query as per the table name :slight_smile:

(From row In DT.AsEnumerable() Group row By INV =New With { Key .INVNum = row.Item(“Groupbycolumnname”)} Into INVGroup = Group Select New With {.INVNumber = INV.INVNum, .Sum=INVGroup.Sum(Function® Double.Parse(r.Item(“SummationColumnName”).ToString()))}).ToList

2 Likes

Thanks a lot @Shubham_Varshney :slight_smile: i got output as Ienurable, now do i need to convert into datatable ?

1 Like

Yup, you can do a CopytoDatatable at end instead of ToList

Thanks @Shubham_Varshney , but when i do it, it throws an error following…
err1

(From row In grpDT.AsEnumerable() Group row By INV =New With { Key .INVNum = row.Item(“Category”)} Into INVGroup = Group Select New With {.Category = INV.INVNum, .AmountSpent=INVGroup.Sum(Function® Double.Parse(r.Item(“Amount”).ToString()))}).ToList.CopytoDatatable()

1 Like

(From row In grpDT.AsEnumerable() Group row By INV =New With { Key .INVNum = row.Item(“Category”)} Into INVGroup = Group Select New With {.Category = INV.INVNum, .AmountSpent=INVGroup.Sum(Function r Double.Parse(r.Item(“Amount”).ToString()))}).CopytoDatatable

1 Like

still same issue bro, copydatatable is not member of

I’ll check that out later, pardon me I am away from my system!!!

I will update the query and share it with you, for the meanwhile please use the ToList query :slight_smile:

1 Like

Thanks a lot :slight_smile:

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