[英]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,但您混合了两个不同的提供程序:
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.