Linq Query: To Filter the Data and Get the SUM of Another Column

How can I sum a certain excel column depending on a filter? I need a Linq query for this. For example, suppose I have a column named “STATUS” that has “ERROR” and “DONE” values, and a second column that contains some integer values. I want to filter on the status column with the value “DONE” and add the second column

Can anyone assist me with this?

Hi @Peter3 ,

This query filters the rows in the DataTable where the STATUS column has the value “DONE”, converts the values in the Column2 column of these rows to double, and then calculates the sum of these double values.

(From row In dt_input.AsEnumerable() Where row("STATUS").ToString() ="DONE" Select Cdbl(row("Column2"))).Sum()

Vinit Mhatre

1 Like

Hi @Peter3

Can you try the below

sumValue = dataTable.AsEnumerable() _
                    .Where(Function(row) row.Field(Of String)("STATUS") = "DONE") _
                    .Sum(Function(row) row.Field(Of Integer)("SecondColumnName"))


1 Like

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