我正在尝试使用LINQ将product_name
与product_prize_kg
连接起来,以便我可以将其显示为下拉列表中的一个字段。当我尝试这样做时,我收到以下错误。
类型'system.collections.generic.list(匿名类型)'的值 无法转换为...
我的代码是这样的:
Public Function selectAll() As List(Of tblProduct)
Dim result = From product In dc.tblProducts
Select New With
{
Key .productID = product.pk_product_id,
Key .productNameKg = Convert.ToString(product.product_name) & " " & Convert.ToString(product.product_price_kg)
}
Return result.ToList()
End Function
这是我要填写的下拉列表。
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="productNameKg"
DataValueField="productID">
</asp:DropDownList>
由于
Grtz Simke
答案 0 :(得分:1)
Public Function selectAll() As List(Of Product)
Dim result = From product In tblProducts
Select New Product With
{
.ProductId = product.pk_product_id,
.ProductNameKg = Convert.ToString(product.product_name) & " " & Convert.ToString(product.product_price_kg)
}
Return result.ToList()
End Function
Public Class Product
Property ProductId As String
Property ProductNameKg As String
End Class