LINQ provides many query operators and custom operators with which we can query DataSets
. When we say querying DataSets
, we actually mean querying DataTables
inside DataSets
. We cannot directly query DataTables
, as it returns DataRow
objects. To be a part of LINQ queries, DataTables
should be enumerable and the source for the LINQ query should be IEnumerable<T>
. Querying can be done on enumeration of DataRow
objects so that we will have all DataColumns
available for the query expressions.
var categories = dataSetDeserts.Tables[0].AsEnumerable(); var items = dataSetDeserts.Tables[1].AsEnumerable(); var rowCategories = from p in categories where p.Field<int>("CategoryID") == 1 select p; foreach (var cat in rowCategories) { Console.WriteLine(cat[0] + " " + cat[1] + " " + cat[2]); }
In the above example, dataSetDeserts
has two tables which have details of different categories and items for each category. To query these details, we need to get the enumeration of...