gpt4 book ai didi

asp.net - LINQ:不支持查询运算符 'ElementAtOrDefault'

转载 作者:行者123 更新时间:2023-12-04 14:06:22 27 4
gpt4 key购买 nike

为什么下面的代码会产生错误?

The query operator 'ElementAtOrDefault' is not supported


Dim Im = (From view In Db.Views Where _
view.Pass = txtCode.Text _
Select New With {.Id = view.UniqueID.ToString}_
).Distinct

Response.Redirect("~/test.aspx?x=" & Im(0).Id)

有没有办法在不使用 FirstOrDefault 选项的情况下修复它?

更新:这是 StackTrace
   at System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
at System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
at System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.ElementAtOrDefault[TSource](IQueryable`1 source, Int32 index)
at Login.btnLogin_Click(Object sender, EventArgs e) in D:\Projects\Memoria\Login.aspx.vb:line 14
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

最佳答案

您需要做的是添加.ToList()到您的查询结束。这应该有效:

Dim Im = (From view In Db.Views Where _
view.Pass = txtCode.Text _
Select New With {.Id = view.UniqueID.ToString}_
).Distinct.ToList()

Response.Redirect("~/test.aspx?x=" & Im(0).Id)

.ToList() ,查询只返回 DataQuery(Of T) 而不是 List(Of T)。添加 ToList 调用有两件事:
  • 强制查询立即执行,
  • 返回支持 ElementAtOrDefault() 的集合类型

  • 希望有帮助!

    关于asp.net - LINQ:不支持查询运算符 'ElementAtOrDefault',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4128180/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com