我正在接受下面引用中提到的例外情况。我试图删除这个问题很多,但我得到下面的例外。我已经阅读了类似于堆栈溢出问题的类似答案,但他们没有解决我的问题。我也在上面评论了我接受例外的行。
System.NotSupportedException未被用户代码
处理 HResult = -2146233067 Message = LINQ to Entities无法识别 方法' System.String ElementAt的[字符串](System.Collections.Generic.IEnumerable1[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(Expression LINQ) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.UnaryTranslator.TypedTranslate(ExpressionConverter) 父,UnaryExpression linq) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator1.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.Translate(ExpressionConverter) 父母,表达linq) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression LINQ) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression输入) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression输入,DbExpressionBinding&捆绑) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter) parent,MethodCallExpression call,DbExpression&资源, DbExpressionBinding&安培; sourceBinding,DbExpression&拉姆达) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter) parent,MethodCallExpression调用) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter) parent,MethodCallExpression调用,SequenceMethod sequenceMethod) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter) parent,MethodCallExpression linq) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator1.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.Translate(ExpressionConverter) 父母,表达linq) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression LINQ) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateSet(Expression LINQ) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter) parent,MethodCallExpression调用) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter) parent,MethodCallExpression调用,SequenceMethod sequenceMethod) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter) parent,MethodCallExpression linq) 在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator1.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 forMergeOption) 在System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
1 func,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess) 在System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func
1 操作) 在System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption) 在System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator
1.MoveNext() 在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable1 source) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__1[TResult](IEnumerable
1 序列) at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle [TResult](IEnumerable1 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 source) 在Neon.DBHandler.registerStudentCourses(String p,List`1 coursesIdSelected)in C:\用户\ AbdullahABBASI \桌面\ AbbasiWebProject \霓虹灯\模型\ DBHandler.cs:线 246 在Neon.Student_RegisterCourse.registerbutton_Click(Object sender,EventArgs e)in C:\用户\ AbdullahABBASI \桌面\ AbbasiWebProject \霓虹灯\ Student_RegisterCourse.aspx.cs:行 51 在System.Web.UI.WebControls.Button.OnClick(EventArgs e) 在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument) 在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
的InnerException:
代码如下:
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;
}
答案 0 :(得分:8)
实体框架无法将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()));