2013-10-11 235 views
0

找不出LINQ查詢問題請告訴我問題與匿名類型

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

,因爲該類型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) 
+0

對您有幫助或沒有? – Ric