【问题标题】:ASP.Net/VB.Net/LINQ: Unable to cast object of type .objectquery to type generic.listASP.Net/VB.Net/LINQ:无法将 .objectquery 类型的对象转换为 generic.list 类型
【发布时间】:2011-09-12 10:04:58
【问题描述】:

我有一个关于 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

【问题讨论】:

    标签: asp.net vb.net linq


    【解决方案1】:

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

    Select products.Product
    

    而不是

    Select products
    

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

    【讨论】:

    • 哇,这对我来说只是一个巨大的愚蠢错误,我想获得 products.Product_no。非常感谢,我原以为结果只是产品。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多