Linq query to count a field in datatable

I have a datatable that contains a column like "Column-A". Now, if this column contains the value "Y" or "N", I need to set the variable count.

for this i need to check if count is greater than 0 or not. How can you achieve with LINQ?

Please, help!

+5
source share
1 answer

Try this, it will count the number of lines containing "Y" or "N" inside Column-A:

int count = dataTable.AsEnumerable()
               .Count(row => row.Field<string>("Column-A") == "Y"
                          || row.Field<string>("Column-A") == "N");

I think this is what you are trying to do? If I misunderstood your question, please let me know.

+6
source

All Articles