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

Android + SQL Server + jTDS

java android sql-server

I'm trying to build an android app with connection to our SQL Server. I have downloaded the jTDS JDBC Driver. I have an jar file and have to copy it to my application's classpath. But where is that...

PHP return object variables inside of an array

php arrays json multidimensional-array

I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure ou...