query dataset with linq

Hi

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

i.e.

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

Open in new window


How can I achieve this ?
websssAsked:
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
                     .OfType<DataTable>()
                     .FirstOrDefault(table => table.TableName == "X")
                     .AsEnumerable()
                     .Where(row => row.Field<int>("eventId") == 1);

Open in new window

0
 
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
0
 
websssAuthor Commented:
thanks
that code works with a data table and not data set
0
 
NorieVBA ExpertCommented:
websss

Which table in the dataset do you want to query?
1
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.