At a client I recently found myself wondering how one can access the data an EntityDataSource returns.

For most applications this scenario does not come up, but I was now curious so I couldn’t just let it go.

I should point out right away that the easiest way to get at the data is to hook the “Selected” event of the EntityDataSource, then you can get the data like this:

protected void EntityDataSource1_Selected(object sender, EntityDataSourceSelectedEventArgs e)
        {
            List<Category> a = e.Results.Cast<Category>().ToList();
            string s = a[1].CategoryName;

        }

(supposing you have an entity/table named Category).

Now that is all fine, but how do you trigger the data to be fetched (execute the Select command) without involving a data bound control like a GridView.

The key lies in being able to cast an EntityDataSource as an IDataSource, and then executing “GetView” from that interface to return a DataSourceView, which can then be made to return a strongly typed list of entities.

The use of the EntityDataSourceReader is as follows:

EntityDataSourceReader<Category> dataReader = new EntityDataSourceReader<Category>(EntityDataSource1);
List<Category> list =  dataReader.GetData();
 

The full code to my EntityDataSourceReader can be found in the source file linked below.

(source)