C Sharper
C Sharper

Reputation: 8626

cast is not a member of system.data.rowcollection

I wanted to select top 5 rows from datatable.

For that i used following query:

DTResult
    .Rows
    .Cast(Of System.Data.DataRow)()
    .Take(Integer.Parse(ddlPage.SelectedValue.ToString()))

But its giving me following error:

cast is not a member of system.data.rowcollection

I removed cast and made it like:

DTResult.AsEnumerable.Take(Integer.Parse(ddlPage.SelectedValue.ToString()))

But in both case it gives me error, and also:

its not autosuggesting .Take()

plz help me.

Upvotes: 0

Views: 1895

Answers (2)

sloth
sloth

Reputation: 101032

To use LINQ (e.g. Take() or Cast()) and the DataTableExtensions (e.g. dataTable.AsEnumerable()), ensure you have referenced the right assemblies and importet the right namespaces.

The assemblies are System.Core.dll for LINQ , and System.Data.DataSetExtensions.dll for the DataTableExtensions.

The namespaces you need are System.Linq and System.Data.

Upvotes: 1

Swapnil Ganjegaonkar
Swapnil Ganjegaonkar

Reputation: 116

In framework 3.5,

dt.Rows.Cast<System.Data.DataRow>().Take(5)

Otherwise use following code

 public DataTable  SelectTopDataRow(DataTable dt, int count)
{
 DataTable dtn = dt.Clone();
 for (int i = 0; i < count; i++)
 {
     dtn.ImportRow(dt.Rows[i]);
 }

 return dtn;
}

Upvotes: 0

Related Questions