首页 > 解决方案 > 使用正确加载的列表对象时接收到 LINQ 表达式 x 无法翻译错误

问题描述

我有以下List对象,它正确加载了数据库中的有效值:

  List<AssessmentItem> assessmentItems = 
      _context.AssessmentItems
              .Include(ai => ai.Assessment).ThenInclude(assess => assess.Evaluator)
              .Where(ai => ai.IsActive &&
                           ai.Assessment.SubmissionId == submissionId &&
                           ai.Assessment.RubricId == rubricId)
              .ToList();

然后我需要assessmentItems在另一个 LINQ 语句中使用这个列表对象。我在下面提供了assessmentItems使用的部分代码。

   ....
   Description = ri.Description,
   Abbreviation = ri.Abbreviation,
   RubricId = ri.RubricId,
   RubricItemCategoryId = ri.RubricItemCategoryId,
   CurrentScores = assessmentItems
                         .Where(aitem => aitem.RubricItemId == ri.Id)
                         .Select(aitem => new AssessmentItemDTO
                         {
                             Id = aitem.Id,
                             EvaluatorName = aitem.Assessment.Evaluator.FirstName + " " + aitem.Assessment.Evaluator.LastName,
                             EvaluatorId = aitem.Assessment.EvaluatorId,
                             CurrentScore = aitem.CurrentScore
                         }).ToList(),

由于某种原因,我收到以下错误:

'aitem'无法翻译LINQ 表达式。以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估

我检查了 SO 中的其他相关帖子,但是我找不到适合我的案例的解决方案。我正在使用最新版本的 EF Core。有什么帮助吗?

标签: c#entity-frameworklinqentity-framework-core

解决方案


推荐阅读