سؤال

I'm trying to grab the entity on the Selected event of the datasource like so

protected void edsRetailer_OnSelected(object sender, EntityDataSourceSelectedEventArgs e)
{
    if(e.Results == null) return;
    var list =  (IEnumerable<Retailer>) e.Results;
}

The cast fails with the following error

Unable to cast object of type 'System.Data.Objects.ObjectView`1[CCBusiness.Retailer]' to type 'System.Collections.Generic.IEnumerable`1[CCBusiness.Retailer]'

I tried casting it to an ObjectView, but the class doesn't seem to exist when I try to cast it.

هل كانت مفيدة؟

المحلول

You don't have to cast e.Results to ObjectView, because that already is the return type. (Besides, System.Data.Objects.ObjectView is an internal class). But the point is that it implements IEnumrable (as part of IBindingList), not IEnuerable<T>.

The common way to convert a non-generic IEnumerable to a generic one is the Cast<T> method:

var list =  e.Results.Cast<Retailer>();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top