---
title: "LINQ Data Table Extensions"
published_at: "2009-07-10T22:31:00+00:00"
modified_at: "2026-05-15T11:48:10+00:00"
url: "https://www.syncfusion.com/blogs/post/linq-data-table-extensions"
excerpt: "Legacy applications, which are to be migrated to a new platform (WPF, Silverlight, etc.) require leveraging the underlying business layers too. We have an IQueryable interface implemented for DataTable in System.Data.DataSetExtensions. There are a couple of extension providers present in..."
taxonomy_category:
  - "LINQ"
taxonomy_post_tag:
  - ".NET"
  - "Data Table Extensions"
  - "LINQ"
---

# LINQ Data Table Extensions

[silverlight](https://www.syncfusion.com/blogs/author/silverlight)

![Image](https://www.syncfusion.com/blogs/wp-content/uploads/2018/08/linq_datatable_be13a11c.png)

Legacy applications, which are to be migrated to a new platform (WPF, Silverlight, etc.) require leveraging the underlying business layers too. We have an IQueryable interface implemented for DataTable in System.Data.DataSetExtensions. There are a couple of extension providers present in the .NET Framework itself:

- DataTableExtensions
- EnumerableRowCollectionExtensions

**Data Table Extensions**

This provides a list of functions to query with the DataTable. Most of them are casting functions:

- **AsDataView<T>** — Accepts an EnumerableRowCollection and returns a DataView.
- **AsDataView** — Accepts a DataTable and returns a DataView.
- **AsEnumerable** — Accepts a DataTable and returns an EnumerableRowCollection, which is used for querying with the DataRow collection.

**Enumerable Row Collection Extensions**

The LINQ provider is implemented for the DataRowCollection that is present in the DataTable.Rows property. The following are the LINQ extensions you can work with:

- Select
- OrderBy
- OrderByDescending
- ThenBy
- ThenByDescending
- Where
- Cast

**Select**

Use the Select extension to provide the query with the required fields from the DataTable.

```
private static void SelectDataTable()
        {
            var dt = GetOrdersDataTable();
            var orders = dt.AsEnumerable().Select(o =>
                new
                {
                    OrderID = o.Field("OrderID"),
                    CustomerID = o.Field("CustomerID"),
                    EmployeeID = o.Field("EmployeeID"),
                    OrderDate = o.Field("OrderDate"),
                    ShipCountry = o.Field("ShipCountry")
                });
            foreach (var order in orders)
            {
                Console.WriteLine(string.Format("OrderID : {0} / CustomerID : {1} 
/ EmployeeID : {2} / OrderDate : {3} / ShipCountry : {4}", order.OrderID, order.CustomerID, 
order.EmployeeID, order.OrderDate, order.ShipCountry));
            }
        }
```

The Field<T> returns a strongly typed value from the underlying DataTable. You can also use SetField<T>

to set the field value thru a strongly typed object.

**OrderBy/OrderByDescending/ThenBy /ThenByDescending**

Sort operations can be performed by using the above functions. Check out the code below:

```
private static void SortDataTable()
{
    var dt = GetOrdersDataTable();
    var orders = dt.AsEnumerable().OrderBy(r => r.Field("ShipCountry"));
    var result = orders.ThenBy(r => r.Field("CustomerID")).Select(o =>
        new
        {
            OrderID = o.Field("OrderID"),
            CustomerID = o.Field("CustomerID"),
            EmployeeID = o.Field("EmployeeID"),
            OrderDate = o.Field("OrderDate"),
            ShipCountry = o.Field("ShipCountry")
        });
    foreach (var order in result)
    {
        Console.WriteLine(string.Format("OrderID : {0} / CustomerID : {1} / EmployeeID : {2} 
/ OrderDate : {3} / ShipCountry : {4}", order.OrderID, order.CustomerID, order.EmployeeID, 
order.OrderDate, order.ShipCountry));
    }
}
```

**Where**

Filter operations require a predicate match to be passed. See the code below:

```
private static void WhereOperation()
{
    var dt = GetOrdersDataTable();
    var filteredOrders = dt.AsEnumerable().Where(o => o.Field("ShipCountry") 
== "Brazil")
.Select(o =>
        new
        {
            OrderID = o.Field("OrderID"),
            CustomerID = o.Field("CustomerID"),
            EmployeeID = o.Field("EmployeeID"),
            OrderDate = o.Field("OrderDate"),
            ShipCountry = o.Field("ShipCountry")
        });
    foreach (var order in filteredOrders)
    {
        Console.WriteLine(string.Format("OrderID : {0} / CustomerID : {1} / EmployeeID : {2} 
/ OrderDate : {3} / ShipCountry : {4}", order.OrderID, order.CustomerID, 
order.EmployeeID, order.OrderDate, order.ShipCountry));
    }
}
```

With these functions we can easily get expressions to work with legacy DataTable objects.

**Note: The API documentation suggests that these APIs are used internally in the .NET Framework and are not intended to be used in our code directly :).**

Hope this helps.
