Search code examples
entity-frameworklinqc#-4.0sql-to-linq-conversion

why my Linqued query is not getting executed


I have this linqued query

  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();

it gives error LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression. on line 3, i.e

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

as I am converting the string to int during inner join


Solution

  • Just saw your 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 how to sort varchar column containing numeric values with linq lambdas to Entity to solve the issue.

    Just replace

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

    with

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