[英]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 我有一个关于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]'.
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.) (您的命名也有些不幸,因为products
仍只引用一个产品-我个人会将该范围变量重命名为product
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.