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

时间:2016-05-11 09:24:51

标签: entity-framework linq c#-4.0 sql-to-linq-conversion

我有这个linqued查询

  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无法识别方法' Int32 ToInt32(System.String)'方法,并且此方法无法转换为商店表达式。 在第3行,即

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

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

1 个答案:

答案 0 :(得分:2)

刚看到你的related question。您的EmployeeNumber字段似乎填充了固定大小(5)零左填充字符串表示的数字。如果这是真的,您可以使用how to sort varchar column containing numeric values with linq lambdas to Entity中的技巧来解决问题。

只需替换

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

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