ASP.Net/VB.Net/LINQ:无法将.objectquery类型的对象转换为类型generic.list

时间:2011-09-12 10:04:58

标签: asp.net vb.net linq

我有一个关于ASP.Net,visual basic的问题 我有2个LINQ查询,第一个有效,第二个无效,生成

"Unable to cast object of type 'System.Data.Objects.ObjectQuery'1[SelmaV2.Products]' to type 'System.Collections.Generic.List'1[System.String]'.

以下是代码:

Sub GetProducts(ByRef productDropList As DropDownList)
    Dim productList As New List(Of String)
    Using context As New SelmaEntities
        productList = (From products In context.Products
                       Order By products.Product
                       Select products.Product).Distinct.ToList()
    End Using
    productDropList.DataSource = productList
End Sub
Sub GetProductNames(ByRef ProductNamesList As List(Of String), ByVal CurrentProduct As String)
    Using context As New SelmaEntities
        ProductNamesList = (From products In context.Products
                            Where products.Product = CurrentProduct
                            Order By products.Product_no Ascending
                            Select products).ToList
    End Using
End Sub

1 个答案:

答案 0 :(得分:0)

您正在尝试获取字符串列表,但看起来您正在选择产品列表。我怀疑你想要像

这样的东西
Select products.Product

而不是

Select products

在查询中。 (你的命名也有点不幸,因为products一次只会引用一个产品 - 我会将该范围变量重命名为product,个人而言。)