Harun
Harun

Reputation: 5179

How do i filter empty rows from a datatable without using a loop?

I've scenario where the datatable may contain large number of rows. As a result i am not able to iterate and update the datatable using a loop.

I've tried the following code,

          from row in table.AsEnumerable()
          where table.Columns.Any(col => !row.IsNull(col))
          select row;

But i can't find definition for Any(). Is there any namespace i should use to get Any()?

Any body please tell me how to correct this or suggest any alternate solutions..

Upvotes: 4

Views: 2673

Answers (4)

DeveloperX
DeveloperX

Reputation: 4683

use system.linq namespace if framework version is greater than 2

Upvotes: -1

ChrisWue
ChrisWue

Reputation: 19070

Apart from having to use the System.Linq namespace you also need to make it aware of the element types. DataTable.Columns is not a generic collection (it implements only IEnumerable not IEnumerable<T>) and the compiler cannot infer the type. You need to do something like this:

from row in table.AsEnumerable()
where table.Columns.Cast<DataColumn>.Any(col => !row.IsNull(col))
select row;

Upvotes: 7

ub1k
ub1k

Reputation: 1674

you should include the following:

using System.Linq;

Upvotes: -1

NinjaNye
NinjaNye

Reputation: 7126

The Any() method is in the System.Linq namespace. The Method lives in the Queryable class

Upvotes: -1

Related Questions