Onel Sarmiento
Onel Sarmiento

Reputation: 1626

How to use LINQ in DataTable?

I'm trying to use LINQ in DataTable and I use Ntier for my solution, But when I'm try to use LINQ in DataTable it gives me an error: Sorry for my english

Error   1   Cannot convert lambda expression to type 'string' because it is not a delegate type c:\users\ba-ojt\documents\visual studio 2010\Projects\GoActiveDirectory\PresentationTier\Default.aspx.cs    21  25  PresentationTier

How can I fix this and how it become a lambda expression?

MyCode:

protected void Page_Load(object sender, EventArgs e)
    {
        DataServiceReference.Service1Client newService = new DataServiceReference.Service1Client();
        DataTable dt = newService.GetAccounts();

        var query = from data in dt
                    select data;//Error Here

        Repeater1.DataSource = query;
        Repeater1.DataBind();

    }

MyReference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.Entity;

Upvotes: 1

Views: 944

Answers (2)

Arion
Arion

Reputation: 31249

Use AsEnumerable() like this:

var query = from data in dt.AsEnumerable()
            select data;

Update: Then to get the columns from the datatable you can do this:

data.Field<YourType>("YourColumnName")

Reference:

Upvotes: 5

IVAAAN123
IVAAAN123

Reputation: 567

Try this:

var query = (from DataRow data in dt.Rows
            select data).ToList();

Upvotes: 1

Related Questions