【发布时间】:2010-09-24 03:56:20
【问题描述】:
我有一个 Linq-to-SQL 数据模型,其中包含一个类型 Party,它为 Company 和 Individual 分类了两次。我正在尝试将两个转发器绑定到 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()
当我按预期单步执行代码时,oComp 和 oInd 是 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, IEnumerable1 序列类型的构造函数) 在 System.Data.Linq.DataQuery1.GetNewBindingList() at System.Data.Linq.DataQuery1.System.ComponentModel.IListSource.GetList() 在 System.Web.UI.DataSourceHelper.GetResolvedDataSource(对象数据源,字符串数据成员) 在 System.Web.UI.WebControls.ReadOnlyDataSource.System.Web.UI.IDataSource.GetView(字符串 viewName) 在 System.Web.UI.WebControls.Repeater.ConnectToDataSourceView() 在 System.Web.UI.WebControls.Repeater.GetData() 在 System.Web.UI.WebControls.Repeater.CreateControlHierarchy(布尔值 useDataSource) 在 System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) 在 System.Web.UI.WebControls.Repeater.DataBind() 在 \parties.aspx.vb:line 491 中的party.lbHiddenPostback_Click(Object sender, EventArgs e) 在 System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) 在 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,字符串 eventArgument) 在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint) 内部异常:
如果我随后选择所有内容作为聚会,而不是如下所示,一切正常
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()
这两个转发器中都没有与返回的数据相关的任何内容,只是项目模板中的一个标签,用于显示每个查询返回了多少条记录。
必须绑定到父类型对我来说没有意义,如果不先转换为该类型,我将无法访问与个人和公司关联的属性!
【问题讨论】:
标签: asp.net vb.net linq .net-3.5