为什么我的 Linqued 查询没有被执行

why my Linqued query is not getting executed

我有这个链接查询

  var moreThen1dayLeavefed = (from LApp in db.LeaveApplications
                                 join Emp in db.Employees
                                on LApp.Employee equals Convert.ToInt32(Emp.EmployeeNumber)
                                 join LBrk in db.LeaveBreakups
                                on LApp.Id equals LBrk.LeaveApplication
                                 where Emp.Team == 8 && LBrk.StartDate.Year == 2015 && LBrk.StartDate.Month == 5 
                                 select new { StartDate = LBrk.StartDate.Day, EndDate = LBrk.EndDate.Day, diff = (DbFunctions.DiffDays(LBrk.StartDate, LBrk.EndDate) + 1) }).ToList();

报错 LINQ to Entities 不识别 method 'Int32 ToInt32(System.String)' 方法,并且无法将此方法转换为存储表达式。 在第 3 行,即

 on LApp.Employee equals Convert.ToInt32(Emp.EmployeeNumber)

因为我在内部连接期间将字符串转换为 int

刚刚看到您的 related question. Your EmployeeNumber field seems to be filled with fixed size (5) zero left padded string representation of a number. If that's true, you can use the trick from 解决了这个问题。

只需更换

on LApp.Employee equals Convert.ToInt32(Emp.EmployeeNumber)

on DbFunctions.Right("0000" + LApp.Employee.ToString(), 5) equals Emp.EmployeeNumber