views:

318

answers:

3

When I write a code like below, I take this error message: "The query operator 'ElementAtOrDefault' is not supported."

How can I fix it?

Dim tmpQuestion As New UIData
    Dim strViews = From t In tmpQuestion.LU_QUESTIONs _
                      Where t.ID_QUESTION = Request.QueryString("IdQuestion") _
                      Select t
    Dim mtViews = strViews(0).MT_VIEWS
A: 

I am no expert in VB or LINQ2SQL but does this not have anything to do with the fact that you say strViews(0).MT_VIEWS while there is a chance that strViews can be null?

Johannes
+3  A: 

Have you tried using the FirstOrDefault() then check to make sure it is not null. My VB syntax is probably suspect, but you get the idea.

Dim strView = (From t In tmpQuestion.LU_QUESTIONS _
                    Where t.ID_QUESTION = Request.QueryString("IdQuestion") _
                    Select t).FirstOrDefault()

Dim mtViews as ...
If Not strView Is Nothing
   mtViews = strView.MT_VIEWS
EndIf
tvanfosson
+3  A: 

You haven't really queried yet. strViews isn't the result set, it is the query. You need to actually retrieve some data.

var chosen = strViews.FirstOrDefault();
toast
Good point about the late binding -- that can really mess you up if you're not careful.
Adam Lassek
Thanks, this sentence is very important:"strViews isn't the result set, it is the query."
mavera