在asp.net中绑定到linq继承的类型

时间:2008-11-26 14:15:57

标签: asp.net vb.net linq .net-3.5

我有一个Linq-to-SQL数据模型,其中包含Party类型,对CompanyIndividual进行了两次细分。我试图将两个转发器绑定到Linq-to-SQL查询,如下所示

    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()

当我按预期逐步执行代码oCompoInd IEnumerable<Company>IEnumerable<Individual>时,但在第一次DataBind调用时,我收到以下异常到达

  

System.MissingMethodException未被用户代码处理     Message =“类型上的构造函数”System.Data.Linq.Provider.DataBindingList 1[[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, IEnumerable 1个序列)          at System.Data.Linq.DataQuery 1.GetNewBindingList() at System.Data.Linq.DataQuery 1.System.ComponentModel.IListSource.GetList()          在System.Web.UI.DataSourceHelper.GetResolvedDataSource(Object dataSource,String dataMember)          在System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(String viewName)          在System.Web.UI.WebControls.Repeater.ConnectToDataSourceView()          在System.Web.UI.WebControls.Repeater.GetData()          在System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource)          在System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e)          在System.Web.UI.WebControls.Repeater.DataBind()          at parties.lbHiddenPostback_Click(Object sender,EventArgs e)在\ parties.aspx.vb中:第491行          在System.Web.UI.WebControls.LinkBut​​ton.OnClick(EventArgs e)          在System.Web.UI.WebControls.LinkBut​​ton.RaisePostBackEvent(String eventArgument)          在System.Web.UI.WebControls.LinkBut​​ton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)          在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)          在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)          在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)     InnerException:

如果我然后按照以下方式选择所有内容,那么一切正常

    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()

任何一个转发器中都没有与返回的数据相关的内容,只是项目模板中的一个标签,显示每个查询返回的记录数。

对我来说,我必须绑定到父类型是没有意义的,我将无法在不首先转换为此类型的情况下访问与Individual和Company关联的属性!

1 个答案:

答案 0 :(得分:1)

您可以在设置转发器的.ToArray ()属性时使用DataSource来解决此问题...

rptCompanies.DataSource = oComp.ToArray ()

我不相信我知道为什么它有效 - 但我尝试过它似乎解决了这个问题!