A DataTable can be queried with LINQ, just as any other IEnumerable<T> list.
Note: DataTable does not implement IEnumerable<T>. You have to call AsEnumerable, which is an extension method for DataTable, to obtain a wrapper that implements that interface.
The list is made of DataRow objects; thus, you must access DataRow member properties to get a field value. This arrangement allows the call of any DataRow member instead of using a query expression over a DataTable. You can use the Field<T> accessor method instead of using a direct cast on the result of the standard DataRow accessor (such as o["OrderDate"]). The query shown in below code gets the orders that show a date of 1998 or later.
Querying a DataTable with LINQ
- DataSet ds = LoadDataSetUsingDataAdapter();
- DataTable orders = ds.Tables["Orders"];DataTable orderDetails = ds.Tables["OrderDetails"];
- var query = from o in orders.AsEnumerable()
- where o.Field < DateTime > ("OrderDate").Year >= 1998
- orderby o.Field < DateTime > ("OrderDate") descending
- select o;
Note: AsEnumerable and Field<T> are two custom extension methods for DataTable and DataRow types. They are defined in System.Data.DataTableExtensions and System.Data.DataRowExtensions, respectively.
When you have several DataTable objects in a DataSet, you might want to use some type of join. The query shown in below code calculates the total order amount for each order from 1998 to the present.
Joining two DataTable objects with LINQ
- DataSet ds = LoadDataSetUsingDataAdapter();
- DataTable orders = ds.Tables["Orders"];
- DataTable orderDetails = ds.Tables["OrderDetails"];
- var query = from o in orders.AsEnumerable()
- join od in orderDetails.AsEnumerable()
- on o.Field < int > ("OrderID") equals od.Field < int > ("OrderID")
- into orderLines
- where o.Field < DateTime > ("OrderDate").Year >= 1998
- orderby o.Field < DateTime > ("OrderDate") descending
- select new
- {
- OrderID = o.Field < int > ("OrderID"),
- OrderDate = o.Field < DateTime > ("OrderDate"),
- Amount = orderLines.Sum(
- od => od.Field < decimal > ("UnitPrice") *
- od.Field < short > ("Quantity"))
- };
In the previous examples, you specified the relationship between orders and orderDetails through the join syntax. If the DataSet contains information about existing relationships between entities, a LINQ query can take advantage of this. In below ex, we use GetChildRows to get the lines for the order details instead of explicitly joining the two tables.
Leveraging DataSet relationships in LINQ queries:
- DataSet ds = LoadDataSetUsingDataAdapter();
- DataTable orders = ds.Tables["Orders"];
- DataTable orderDetails = ds.Tables["OrderDetails"];
- ds.Relations.Add("OrderDetails", orders.Columns["OrderID"], orderDetails.Columns["OrderID"]);
- var query = from o in orders.AsEnumerable()
- where o.Field < DateTime > ("OrderDate").Year >= 1998
- orderby o.Field < DateTime > ("OrderDate") descending
- select new
- {
- OrderID = o.Field < int > ("OrderID"),
- OrderDate = o.Field < DateTime > ("OrderDate"),
- Amount = o.GetChildRows("OrderDetails").Sum(
- od => od.Field < decimal > ("UnitPrice") *
- od.Field < short > ("Quantity"))
- };