1

我正在尝试使用 VB.NET 加载 Gridview,但是当我执行 Linq 查询时收到以下消息。我正在执行一个存储过程。我检查了 SQL Server 上的 SP,它正在返回结果,但我无法让 VB.Net 接受结果。我对 Linq 和实体框架非常陌生。任何帮助,将不胜感激。

消息=无法将类型“ WhereSelectEnumerableIterator2[WindowsApplication1.uspGetAll_Result ,WindowsApplication1.uspGetAll_Result]”的对象转换为类型“ System.Linq.IQueryable1[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 = ... 行上失败。

4

1 回答 1

0

根据MSDN,数据源必须是以下类型才能绑定到它。

DataGridView 类支持标准的 Windows 窗体数据绑定模型。这意味着数据源可以是实现以下接口之一的任何类型: IList 接口,包括一维数组。IListSource 接口,例如 DataTable 和 DataSet 类。IBindingList 接口,例如 BindingList(Of T) 类。IBindingListView 接口,例如 BindingSource 类。

你有一个IQueryable(Of T)

它继承自 IEnumerable,因此不实现上述任何接口。

使用 .ToList 将其转换为 List(Of T) 将解决您在评论中所述的问题。

gvResults.DataSource = CatFNQuery.ToList()

也许您甚至可以尝试使用 Bindingsource。

Dim bindingSource as New BindingSource()
bindingSource.DataSource = CatFNQuery
gvResults.DataSource = bindingsource

因为绑定源的数据源支持更多类型。

于 2013-11-10T09:14:23.323 回答