Filtering DataSet using linq

Although DataSet provides some querying capabilities using its Select method but the usage is very limited and the result is not strongly typed. Using Linq to DataSet extensions you can apply unlimited querying capabilities and also transform your DataSet into strongly typed objects. Here is an example where I am returning products collection from a DataSet and also applying a filter on the DataSet to return products rows that belong to category of beverage.

image

In the above example, I fill my DataSet using SqlDataAdapter which uses the SQL statement to returns all the products in the NorthWind database. In order to apply linq query over DataSet, I am first converting my DataSet to Enumerable of DataRow objects and then applying the category filter. In order to apply category filter, I need to cast my column in the DataRow to the correct type to compare it against the cateogryid value. In the last portion of the query, I ma simply building an anonymous type from the datarows returned after the filter.

1 Comment

Comments have been disabled for this content.