query dataset with linq


I have used ado.net to return a dataset
I now need to query the dataset with linq


int event1Count = ds.where(eventId = 1);
int event2Count = ds.where(eventId = 2);

Open in new window

How can I achieve this ?
Who is Participating?
käµfm³d 👽Commented:
A DataSet is a collection of DataTables. Just pick the table you want to iterate over from the Tables property. You can do that with LINQ also:

var matchingRows = ds.Tables
                     .FirstOrDefault(table => table.TableName == "X")
                     .Where(row => row.Field<int>("eventId") == 1);

Open in new window

Dorababu MSenior Software EngineerCommented:
You can try as below
var results = from myRow in ds.AsEnumerable()
where myRow.Field<int>("eventId") == 1
select myRow;

Open in new window

You can apply or condition if you want to filter with 2
websssAuthor Commented:
that code works with a data table and not data set
NorieVBA ExpertCommented:

Which table in the dataset do you want to query?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.