Gonzalo.-
Gonzalo.-

Reputation: 12682

Lambdas does not recognize fields of DataTables

I've do this many times in another solutions. But in one of them, It doesn't work. Don't know why.

I want to use Where() in some dataTable. So, I do, for example

int someId;
DatasetName.DatatableName.AsEnumerable().Where(x => x.id == someId);

But in this project, it doesn't recognize the lambda x => x.id == someId

Why is that? I can't use them. The intelissense, afther the ( in the Where recognize that needs to receive a Func, and when I do x => x. it doesn't recognize the fields of the DataTable. Why I cant do x.id ? I know I can use Field<>() (I think that was the name), but I have used the expressions directly a lot of times and I consider them more readables. And in the other solutions does work this.

I have added references of

using System.Linq;    
using System.Data.Linq;
using System.Linq.Expressions;

It doesn't compile, and does not recognize any fields of the Datatable. The project is set for .Net Framework 3.5.

Any help will be appreciated

Upvotes: 0

Views: 329

Answers (2)

Ashfaque Ali Solangi
Ashfaque Ali Solangi

Reputation: 1891

dataSet.Tables[0].AsEnumerable().Where(
r => r["Col1"] == "MyValue")

You can use LINQ to DataSets to do this:

Upvotes: 0

lc.
lc.

Reputation: 116528

I am assuming you are working with a strongly-typed dataset.

The AsEnumerable() is probably returning an IEnumerable<Object> for some reason. Try adding a cast:

DatasetName.DatatableName.AsEnumerable().Cast<MyRowType>().Where(x => x.id == someId);

You should also make sure the DataTable properly inherits from System.Data.TypedTableBase<MyRowType>.

Upvotes: 1

Related Questions