binding to linq inherited types in asp.net

I have a Linq-to-SQL data model that includes a type Party which is sub classed twice for Company and Individual. I am trying to bind two repeaters to Linq-to-SQL queries as follows

    Dim oComp As IEnumerable(Of Company)
    Dim oInd As IEnumerable(Of Individual)

        oComp = From oP As Company In ERM.Parties _
                 Where TypeOf (oP) Is Company And _
                 oP.Name.StartsWith(sSearchString)

        oInd = From oP As Individual In ERM.Parties _
                Where TypeOf (oP) Is Individual And _
                (oP.FirstName.StartsWith(sSearchString) Or _
                oP.LastName.StartsWith(sSearchString))

        rptIndividuals.DataSource = oInd
        rptCompanies.DataSource = oComp

        rptCompanies.DataBind()
        rptIndividuals.DataBind()

when I step through the code oComp and oInd are IEnumerable<Company> and IEnumerable<Individual> as expected, but I get the following exception when the first DataBind call is reached

System.MissingMethodException was unhandled by user code Message="Constructor on type 'System.Data.Linq.Provider.DataBindingList1[[DataModel.Party, DataModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found." Source="mscorlib" StackTrace: at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Data.Linq.Provider.BindingList.Create[T](DataContext context, IEnumerable1 sequence) at System.Data.Linq.DataQuery1.GetNewBindingList() at System.Data.Linq.DataQuery1.System.ComponentModel.IListSource.GetList() at System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource, String dataMember) at System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(String viewName) at System.Web.UI.WebControls.Repeater.ConnectToDataSourceView() at System.Web.UI.WebControls.Repeater.GetData() at System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) at System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) at System.Web.UI.WebControls.Repeater.DataBind() at parties.lbHiddenPostback_Click(Object sender, EventArgs e) in \parties.aspx.vb:line 491 at System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:

if I then select everything as parties instead as follows, it all works ok

    Dim oComp As IEnumerable(Of Party)
    Dim oInd As IEnumerable(Of Party)


        oComp = From oP In ERM.Parties _
                Where TypeOf (oP) Is Company And _
                CType(oP, Company).Name.StartsWith(sSearchString)

        oInd = From oP In ERM.Parties _
                Where TypeOf (oP) Is Individual And _
                (CType(oP, Individual).FirstName.StartsWith(sSearchString) Or _
                CType(oP, Individual).LastName.StartsWith(sSearchString))

        rptIndividuals.DataSource = oInd
        rptCompanies.DataSource = oComp

        rptCompanies.DataBind()
        rptIndividuals.DataBind()

There is nothing in either repeater that relates to the data returned yet, just a label in the item template to show me how many records are returned for each query.

It doesn't make sense to me that I have to bind to the parent type, I will be unable to access the attributes associated with Individual and Company without first casting to this type!

Answers


You can fix this by using .ToArray () when you're setting the DataSource property of the repeater...

rptCompanies.DataSource = oComp.ToArray ()

I'm not convinced I know why it works - but I tried it and it appears to solve the problem!


Need Your Help

DTD: How to declare a DTD to must have a element value?

xml schema dtd

I want to declare my DTD to force the XML node should have a value.

What is required to call java webservice by other clients?

java web-services rest soap jax-rs

I have developed two java web services; one is SOAP based and the second one REST. Now I have to ask some clients (they can be either java/non java clients) to use my web services. Now what informa...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.