4

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..

4 Answers 4

7

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;
Sign up to request clarification or add additional context in comments.

4 Comments

I think it'd be better to use Cast<T>() rather than OfType<T>(). Cast just performs the cast. OfType on the other hand will do additional filtering. Everything in the Columns collection is known to be a DataColumn so it is unnecessary to do the filtering.
@Jeff: Mh, in this case you are probably right. I usualy use OfType when working on non-generic collections as it won't throw on me in case someone put an unexpected object in.
Thanks for the answer.. How can i assign this to another datatable?
@Harun, You could create a new DataTable and use ImportRow to import the filtered rows. Otherwise: Ask a new question and see if someone comes up with a good answer :)
-1

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

Comments

-1

you should include the following:

using System.Linq;

Comments

-1

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

1 Comment

Clearly he is already using the System.Linq namespace (AsEnumerable).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.