[英]Unable to cast object of type 'WhereSelectEnumerableIterator'2 to Person (VB.Net)
[英]VB.NET - Unable to cast object of type 'WhereSelectEnumerableIterator`2
我正在嘗試使用VB.NET加載Gridview,但是執行Linq查詢時,我收到以下消息。 我正在執行存儲過程。 我檢查了SQL Server上的SP,它正在返回結果,但是我無法讓VB.Net接受結果。 我對Linq和Entity Framework非常陌生。 任何幫助,將不勝感激。
消息=無法轉換類型為“
WhereSelectEnumerableIterator2[
WindowsApplication1.uspGetAll_Result,WindowsApplication1.uspGetAll_Result
]”的對象,以鍵入“System.Linq.IQueryable
1 [WindowsApplication1.uspGetAll_Result`]”。
這是代碼。
'Using Linq to Entities
Dim CatFNQuery As IQueryable(Of uspGetAll_Result)
CatFNQuery = From x In fanDbContext.uspGetAll(Nothing, Nothing, Nothing) _
Select x
'MsgBox(CatFNQuery.Count)
If CatFNQuery Is Nothing Then
'
Else
gvResults.DataSource = CatFNQuery
End If
它在CATFNQUERY = ...行上失敗。
根據MSDN ,數據源必須具有以下類型才能綁定到它。
DataGridView類支持標准Windows窗體數據綁定模型。 這意味着數據源可以是實現以下接口之一的任何類型:IList接口,包括一維數組。 IListSource接口,例如DataTable和DataSet類。 IBindingList接口,例如BindingList(Of T)類。 IBindingListView接口,例如BindingSource類。
你有一個IQueryable(T)
它繼承自IEnumerable,因此不實現上述任何接口。
將其轉換為.ToList到List(Of T)將解決您在注釋中陳述的問題。
gvResults.DataSource = CatFNQuery.ToList()
也許您甚至可以嘗試使用Bindingsource。
Dim bindingSource as New BindingSource()
bindingSource.DataSource = CatFNQuery
gvResults.DataSource = bindingsource
由於綁定源的數據源支持更多類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.