简体   繁体   中英

ASP.Net/VB.Net/LINQ: Unable to cast object of type .objectquery to type generic.list

I have a question about ASP.Net, visual basic I have 2 LINQ query's, the first one works, the second one doesnt, produces a

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

Heres the code:

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

Well you're trying to get a list of strings , but it looks like you're selecting a list of products . I suspect you want something like

Select products.Product

instead of

Select products

in the query. (Your naming is also somewhat unfortunate, as products will still only refer to a single product at a time - I would rename that range variable to product , personally.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM