简体   繁体   English

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

[英]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.

相关问题 无法将“X”类型的对象转换为“X”类型 - ASP.NET - Unable to cast object of type 'X' to type 'X' - ASP.NET asp.net无法转换类型错误的对象 - asp.net unable to cast object of type error VB.Net页面中具有ASP.Net导入指令的别名通用类型 - Alias Generic Type With ASP.Net Import Directive in VB.Net Page asp.net无法使用VB将类型为“ System.String”的对象转换为类型为“ System.Data.DataTable” - asp.net Unable to cast object of type 'System.String' to type 'System.Data.DataTable' Using VB 带有VB.NET的简单ASP.NET-无法加载类型 - Simple ASP.NET with VB.NET - Could not load type asp.net错误:无法将'System.DateTime'类型的对象强制转换为'System.String' - asp.net error: Unable to cast object of type 'System.DateTime' to type 'System.String' ASP.Net错误-无法将类型为“ System.String”的对象转换为类型为“ System.Data.DataTable” - ASP.Net Error - Unable to cast object of type 'System.String' to type 'System.Data.DataTable' Asp.net MVC:无法将“Models.RegisterViewModel”类型的 object 转换为“Models.ApplicationUser”类型 - Asp.net MVC: Unable to cast object of type 'Models.RegisterViewModel' to type 'Models.ApplicationUser' 无法将类型为'System.Data.Objects.ObjectQuery`1…的对象转换为类型为'DatabaseModel1…'的对象。 均价 - Unable to cast object of type 'System.Data.Objects.ObjectQuery`1 … to type 'DatabaseModel1…'. ASP ASP.NET:Generic.List(Of T)的内置Gridview排序 - ASP.NET: Built-in Gridview Sorting of Generic.List(Of T)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM