vb.net 数据直接绑定到商店查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29036430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:52:55  来源:igfitidea点击:

Data binding directly to a store query

asp.netvb.netentity-framework

提问by Internet Engineer

I am trying to group amount and load into a drop down box in vb.net (asp.net)

我正在尝试将金额分组并加载到 vb.net (asp.net) 中的下拉框中

But I get the following error:

但我收到以下错误:

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see

不支持直接绑定到商店查询(DbSet、DbQuery、DbSqlQuery、DbRawSqlQuery)的数据。而是使用数据填充 DbSet,例如通过在 DbSet 上调用 Load,然后绑定到本地数据。对于 WPF 绑定到 DbSet.Local。对于 WinForms 绑定到 DbSet.Local.ToBindingList()。对于 ASP.NET WebForms,您可以绑定到在查询上调用 ToList() 的结果或使用模型绑定,有关更多信息,请参见

My Code:

我的代码:

'Load Amounts
Dim SourceAmounts = (From p In db.PayoutAdjustments
                     Order By p.Amount
                     Where p.PayoutId = PayoutId
                     Group p By Key = p.Amount Into Group
                     Select Amount = Group)

cmbAmount.DataSource = SourceAmounts
cmbAmount.DataTextField = "Amount"
cmbAmount.DataValueField = "Amount"
cmbAmount.DataBind()
cmbAmount.Items.Insert(0, New ListItem("Select Amount", 0))

回答by rdans

you need to execute the query before data binding. Using ToList() will force your query to execute.

您需要在数据绑定之前执行查询。使用 ToList() 将强制执行您的查询。

cmbAmount.DataSource = SourceAmounts.ToList()