Linq query to Extract the Matched Values from the Datatable (Not Specific Column)

Example : Column 0 Column1 Column
1 0 1
0 0 1
1 1 0
0 0 1
1 1 0

Need to get the output as Count of 1 as 8

Try this approach:

image

Code

Dim counter As Integer = 0
For Each row As DataRow In dt_DataTable.Rows
    For Each item In row.ItemArray
        If item.ToString() = "1" Then
            counter += 1
        End If
    Next
Next
output = counter

image

1 Like

In general:
Assign acitivty
intSum | DataType: int32 =

(From d in dtData.AsEnumerable
Let ps = d.ItemArray.Sum(Function (x) CInt(x.toString))
Select s = ps).Sum()

we catching the part sum from the row and sum up all partsums afterwards to total sum

we can filter and modify:

Assign Activity:
intFilter | int32 = 1

Assign acitivty
intSum | DataType: int32 =

(From d in dtData.AsEnumerable
From v in d.ItemArray
Let pv = CInt(v.ToString.Trim)
Where pv.Equals(intFilter)
Select r=pc).Sum()

assign Activity:
strReport = String.Format(“Filter: {0} - Sum: {1}”, intFilter.toString, intSum.ToString)

When null / non valid numbers are also to handle we can extend the LINQ

@sandeep.panthulu06

you can also use this

dt_inputdata.AsEnumerable.Sum(Function(a) a.ItemArray.Sum(Function(b) CInt(b.ToString)))

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