LINQ to DataSet Data Binding – Introducing LinqDataView

It's long overdue, but I finally was able to finish a few more posts on this series. Let me know if you find it helpful, and what you would like to see more of!

Why DataView?

As mentioned in this previous post, there are numerous options for data binding with LINQ to DataSet. The post is going to talk about what I feel is the most powerful and flexible option, LinqDataView.


While I will be talking about LinqDataView as a new object, it is in fact the same old DataView that we know and love. The difference is how the DataView is created – so to keep confusion to a minimum, I will talk about DataView and LinqDataView. However, please be aware that if you look for a LinqDataView object, you will not be able to find it! It is an internal-only class that inherits from DataView that is not directly creatable by you, which is OK as you create it by way of a LINQ to DataSet query. Once you create the LinqDataView, you work with it the same way as you would a DataView, because that is what it is.

DataView – Features and benefits

What are some of the advantages of using the LinqDataView? To answer that, we need to understand some of the benefits of the DataView, as the LinqDataView is simply that, a new type of DataView.


The DataView is a filtered and/or sorted version of your live data. If you update the DataTable on which the DataView is based, the changes will appear in the DataView. The same goes if you modify the data in the DataView – the underlying DataTable will be updated.


The DataView can be sorted, and any changes to the data, or the sort, are immediately applied to the DataView. You can either specify the columns used to sort, or for LinqDataView, use an expression for the sorting.


The DataView can also be filtered, and the filtering also benefits from the live nature of the DataView. When the data changes such that certain row no longer should be included in the DataView, or if they now should, the DataView immediately reflects this. This filtering can occur via a string expression or when using LINQ to DataSet, a lambda method.


Finally, the DataView is a concrete representation of an index, which speeds up a number of operations against the DataView. I’ll go into more detail on these operations in a later post.

When to use DataView

DataViews are generally used in one of two places. It is by no means restricted to these scenarios, but these are the main uses.

UI Binding

When a DataTable needs to be bound to UI elements, especially a rich control like a grid, this usually occurs via a DataView. When using a DataView for binding, you gain the ability to sort and filter (think of clicking on a column to sort by that column). Due to the two-way nature of the DataView, you are also able to make edits directly on the DataView and have those changes be reflected in the DataTable, and vice versa.

Fast lookup

A DataView is represented internally by an index, with the index key being the sort criteria. You can use this index to find DataRows within the DataView by using the Find methods. These methods allow you to leverage the index to increase performance of DataRow lookups.

DataView and how LinqDataView differs

The DataView has two key features, filtering and sorting. In versions previous to 3.5, these two features were configured by way of a string. You could specify the filter using the expression language, and specify the column for the sorting.

LinqDataView contains the same operations – filtering and sorting. The difference is that instead of a string based expression or a column name, you can write C# or VB code to do the filtering and sorting. This opens up a whole range of possibilities as to what you can do.

How to create a LinqDataView

To create a LinqDataView, you simply need to write a LINQ to DataSet query. Then, simply call AsDataView on your query, and you get a DataView!

var query = from order in adventureWorksDS1.SalesOrderHeader

            where order.AccountNumber.StartsWith("10-4030")

            orderby order.OrderDate descending

            select order;

DataView view = query.AsDataView();


However, there are some caveats. You can’t just write any LINQ to DataSet query. Because the LinqDataView is a DataView, you can only use it when the query has same structure as a DataView . What does that mean? That means you can filter and sort the query – any other LINQ operators will cause the query to be non-convertible to a DataView. The list of supported LINQ operators is shown below, along with any restrictions.


No restrictions

Order By

No restrictions

Then By

No restrictions


Can only cast to DataRow


Can only be identity select


The wonders of languages

So how do you know when you can make a DataView representation of a query? Try it! If you can’t see the AsDataView method, then you’ve used a LINQ operator that isn’t supported. Generally, if you can call AsDataView, it should just work.

Where do we go from here?

Now that I’ve shown you how to create a LinqDataView, I’m going to spend some time talking about what you can do with it. My next post will talk about restriction joins and leveraging the index for fast lookups.

For an additional example of what you can do with the LinqDataView, check out my webcast on LINQ to DataSet, in particular the last section.