S.aad
S.aad

Reputation: 538

Linq Query Problems with Anonymous Types

Cant Figure Out Whats The Problem

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>)'.   

Upvotes: 0

Views: 424

Answers (1)

Ric
Ric

Reputation: 13248

Because the type is IQueryable you need to enumerate over the query so that it is evaluated and then it can be used.

This should work (note I do not check for Nothing which you should do):

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)

Upvotes: 1

Related Questions