Linq查询匿名类型的问题

时间:2013-10-11 11:06:19

标签: vb.net linq-to-sql

无法弄清楚问题是什么

If IsNothing(_cartItem) Then

    Dim SPDB As New SamplePicturesDataContext()
    Dim q = From sp In SPDB.Pictures _
            Where sp.Id = ItemId _
            Select New With {.Pic_Desc = sp.Description, _
                             .Pic_Title = sp.PictureName}
    _cartItem = New CartItem(q.Pic_Desc, 1, q.Pic_Title)
Else


Error   1   'Pic_Desc' is not a member of 'System.Linq.IQueryable(Of <anonymous type>)'.    

Error   2   'Pic_Title' is not a member of 'System.Linq.IQueryable(Of <anonymous type>)'.   

1 个答案:

答案 0 :(得分:1)

因为类型是IQueryable,所以您需要枚举查询以便对其进行评估,然后才能使用它。

这应该有用(注意我不检查你应该做的Nothing):

Dim SPDB As New SamplePicturesDataContext()
    Dim q = (From sp In SPDB.Pictures _
            Where sp.Id = ItemId _
            Select New With {.Pic_Desc = sp.Description, _
                             .Pic_Title = sp.PictureName}).SingleOrDefault() ' assume singleordefault due to matching on id values.

    _cartItem = New CartItem(q.Pic_Desc, 1, q.Pic_Title)