System.String ElementAt[String](System.Collections.Generic.I

本文关键字:System String Generic Collections ElementAt | 更新日期: 2023-09-27 17:50:48

我收到下面引号中提到的异常。我已经试过很多次消除这个问题,但我得到了下面的异常。我读过关于我的问题的类似答案,但它们并不能解决我的问题。我也在我收到例外的那一行上面发表了评论。

System.NotSupportedException未由用户代码处理
HResult=-2146223067消息=LINQ to Entities无法识别方法"System.String"ElementAt[String](System.Collections.Generic.IEnumerable 1[System.String], Int32)' method, and this method cannot be translated into a store expression. Source=EntityFramework StackTrace: at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.Translate(ExpressionConverter父项,表达式linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq(位于System.Data.Entity.Core.Objects.ELinq.EExpressionConverter.UnaryTranslator.TypedTranslate(ExpressionConverter父级,UnaryExpression linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.Translate(ExpressionConverter parent, Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.翻译(ExpressionConverter父项,表达式linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq(在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpressionlambda,DbExpression输入(在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpressionlambda,DbExpression输入,DbExpressionBinding&绑定(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用,DbExpression&来源DbExpressionBinding&sourceBinding,DbExpression&λ(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用(位于System.Data.Entity.Core.Objects.ELinq.EExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用,SequenceMethod SequenceMethod(位于System.Data.Entity.Core.Objects.ELinq.EExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter父级,MethodCallExpression linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.Translate(ExpressionConverter parent, Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.翻译(ExpressionConverter父项,表达式linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateSet(表达式linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用(位于System.Data.Entity.Core.Objects.ELinq.EExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用,SequenceMethod SequenceMethod(位于System.Data.Entity.Core.Objects.ELinq.EExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter父级,MethodCallExpression linq(位于System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator 1.Translate(ExpressionConverter parent, Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1对于MergeOption(位于System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func 1func,IDbExecutionStrategy executionStrategy,布尔startLocalTransaction,Boolean releaseConnection OnSuccess(位于System.Data.Entity.Core.Objects.ObjectQuery 1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1操作(位于System.Data.Entity.Core.Objects.ObjectQuery 1.GetResults(Nullable 1对于MergeOption(位于System.Data.Entity.Core.Objects.ObjectQuery 1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator 1.MoveNext((位于System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable 1序列(位于System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable 1 query, Expression queryRoot) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable 1源(位于中的Neon.DBHandler.registerStudentCourses(字符串p,列出`1个coursesIdSelected(c: ''Users''AbdullahABBASI''Desktop''AbasiWebProject''Neon''Models''DBHandler.cs:line246在中的Neon.Student_RegisterCourse.registerbutton_Click(Object sender,EventArgs e(c: ''Users''AbdullahABBASI''Desktop''AbasiWebProject''Neon''Student_RegisterCourse.aspx。cs:line51位于System.Web.UI.WebControls.Button.OnClick(EventArgs e(位于System.Web.UI.WebControls.Button.RisePostBackEvent(字符串事件参数(位于System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串eventArgument(位于System.Web.UI.Page.RisePostBackEvent(IPostBackEventHandler源控件,字符串事件参数(位于System.Web.UI.Page.RisePostBackEvent(NameValueCollection postData(在System.Web.UI.Page.ProcessRequestMain上(布尔值包含同步点之前的阶段,布尔值包括同步点之后的阶段(
内部异常:

代码如下:

internal static void registerStudentCourses(string p, List<string> coursesIdSelected)
{
    AbbasiDatabaseEntities objDB = new AbbasiDatabaseEntities();
    Student objStudent = objDB.Students.Where(s => s.Id == p).FirstOrDefault();
    List<Course> courses = new List<Course>();
    for (int i = 0; i<coursesIdSelected.Count; i++)
    {
        //in the line below i am getting exception mentioned above
        var course = objDB.Courses
            .Where(c => c.Id == (string)coursesIdSelected.ElementAt(i))
            .Select(s=> s)
            .FirstOrDefault();
        courses.Add(course);
    }
    objStudent.Courses = courses;    
} 

System.String ElementAt[String](System.Collections.Generic.I

Entity Framework无法将Where(c => c.Id == (string)coursesIdSelected.ElementAt(i))转换为查询。

引入一个辅助变量来从查询中删除该代码:

for (int i = 0; i<coursesIdSelected.Count; i++){
    string selectedCourse = (string)coursesIdSelected.ElementAt(i);
    var course = objDB.Courses.Where(c => c.Id == selectedCourse).Select(s=> s).FirstOrDefault();
    courses.Add(course);
}

或者将整个循环简化为:

student.Courses = db.Courses.Where(c => coursesIdSelected.Contains(c.Id.ToString()));
相关文章: