简体   繁体   English

无法翻译 LINQ 表达式。 通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”的调用来显式评估客户端

[英]LINQ expression could not be translated. client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList'

The error message错误信息

The LINQ expression 'DbSet<Project>()
    .Join(
        inner: __p_0, 
        outerKeySelector: p => p.ProjectId, 
        innerKeySelector: t => t.ProjectId, 
        resultSelector: (p, t) => new ProjectDateModel{ 
            ProjectId = p.ProjectId, 
            ProjectName = p.ProjectName, 
            Date = t.Date 
        }
    )' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

Here is my ef model这是我的 ef 模型

public partial class Task
    {
        public int TaskId { get; set; }
        public int EmployeeId { get; set; }
        public int ProjectId { get; set; }
        public DateTime? Date { get; set; }
    }
public partial class Project
    {
        public int ProjectId { get; set; }
        public string ProjectName { get; set; }
    }

What am trying to achieve here is, select all the tasks in the specific month and select all the projects by task.ProjectId and project.ProjectId My linq expression这里想要实现的是,选择特定月份的所有任务,并通过 task.ProjectId 和 project.ProjectId 选择所有项目我的 linq 表达式

var date = new DateTime(year, month, 1);
        var projectModel = new List<ProjectDateModel>();
        for (int i = 0; i < DateTime.DaysInMonth(year, month); i++)
        {
          var tasks = _context.Task.Where(t => t.Date == date && a.EmployeeId == employeeId).ToList();
          var projects = _context.Project.Join(tasks, p => p.ProjectId, t => t.ProjectId, (p, t) => new ProjectDateModel {
            ProjectId = p.ProjectId,
            ProjectName = p.ProjectName,
            Date = a.Date
          }).ToList();
          projectModel.AddRange(projects);
          date = date.AddDays(1);
        }

        return (projectModel);

ProjectDateModel项目日期模型

public class ProjectDateModel
    {
        public int ProjectId { get; set; }
        public string ProjectName { get; set; }
        public DateTime? Date { get; set; }
    }

The issue is likely that you perform one EF query and call ToList , then try to join that local list with another EF query.问题很可能是您执行一个 EF 查询并调用ToList ,然后尝试将该本地列表与另一个 EF 查询加入。

It's all LINQ but you're mixing two different providers:这都是 LINQ,但您混合了两个不同的提供程序:

  • LINQ to Objects and LINQ to 对象和
  • LINQ to Entities. LINQ 到实体。

Your EF query needs to be able to be translated to SQL code to execute on the database but your local list doesn't exist on the database.您的 EF 查询需要能够转换为 SQL 代码才能在数据库上执行,但您的本地列表在数据库中不存在。 Get rid of the ToList call on the first query and I suspect that it will work.摆脱对第一个查询的ToList调用,我怀疑它会起作用。

Try that way , iam just not sure about date prop试试这种方式,我只是不确定日期道具

var tasks = _context.Tasks
    .Where(x => SqlFunctions.DatePart("month", x.Date) == date.Month && x.EmployeeId == employeeId)
    .ToList();

var taskIds = tasks.Select(x => x.ProjectId);

var projects = _context.Projects
    .Where(x => taskIds.Contains(x.ProjectId))
    .Select(x => new ProjectDateModel
          {
               ProjectId = x.ProjectId,
               ProjectName = x.ProjectName,
               Date = tasks.FirstOrDefault(y => y.ProjectId == x.ProjectId).Date,
          });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法翻译 LINQ 表达式。 要么以可翻译的形式重写查询,要么切换到客户评估 - The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation 要么以可翻译的形式重写查询,要么显式切换到客户端评估……通过插入对 &#39;AsEnumerable 的调用 - Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly… by inserting a call to 'AsEnumerable 无法翻译 LINQ 表达式。 以可翻译的形式重写查询,或切换到客户端评估 EF Core 3.1 - The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1 LINQ 无法翻译表达式。 要么以可以翻译的形式重写查询 - LINQ expression could not be translated. Either rewrite the query in a form that can be translated 无法翻译 LINQ 表达式。 要么以可以翻译的形式重写查询 - The LINQ expression could not be translated. Either rewrite the query in a form that can be translated EF Core 6 - 无法翻译 Linq 表达式。 试图检查链接属性的详细信息 - EF Core 6 - The Linq Expression could not be translated. Trying to check details of linked property 无法翻译 LINQ 表达式 g.Inner)'。 以可以翻译的形式重写查询 - The LINQ expression g.Inner)' could not be translated. Either rewrite the query in a form that can be translate 无法翻译 LINQ 表达式 - The LINQ expression could not be translated LINQ 表达式无法翻译 - LINQ expression could not be translated LINQ 表达式无法翻译 - LINQ Expression could not be translated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM