关于Linq To Sql的一个问题

时间:2022-12-14 09:38:19

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

当我编写如下代码时,我会收到以下错误消息:“不支持查询运算符'ElementAtOrDefault'。”

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

3 个解决方案

#1


3  

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

你还没有真正质疑过。 strViews不是结果集,而是查询。您需要实际检索一些数据。

var chosen = strViews.FirstOrDefault();

#2


3  

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.

您是否尝试过使用FirstOrDefault()然后检查以确保它不为空。我的VB语法可能是可疑的,但你明白了。

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

#3


0  

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?

我不是VB或LINQ2SQL的专家,但这与你说strViews(0).MT_VIEWS的事实没有任何关系,而strViews有可能是null吗?

#1


3  

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

你还没有真正质疑过。 strViews不是结果集,而是查询。您需要实际检索一些数据。

var chosen = strViews.FirstOrDefault();

#2


3  

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.

您是否尝试过使用FirstOrDefault()然后检查以确保它不为空。我的VB语法可能是可疑的,但你明白了。

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

#3


0  

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?

我不是VB或LINQ2SQL的专家,但这与你说strViews(0).MT_VIEWS的事实没有任何关系,而strViews有可能是null吗?