Entity Framework: DbComparisonExpression Requires Arguments With Comparable Types

When working with Entity Framework I got the following error:



Basically what happened is that when you work with Entity Framework, it tries to translate your query into SQL and when doing that it doesn't seem to take into account your type conversion operators. Simply use the following query instead. Change your code as in the following.

From

  1. public List < EmployeeDetailDataContract > EmployeeDetails(string EMP_ID)   
  2. {  
  3.     var query = (from a in ctx.EmployeeDetails  
  4.     where a.Emp_ID.Equals(EMP_ID)  
  5.     select a).Distinct();  
  6.     List < EmployeeDetailDataContract > EMPDetailList = new List < EmployeeDetailDataContract > ();  
  7.     if (query.ToList().Count > 0) {  
  8.         query.ToList().ForEach(rec = > {  
  9.             EMPDetailList.Add(new EmployeeDetailDataContract {  
  10.                 Emp_ID = Convert.ToString(rec.Emp_ID),  
  11.                 Emp_DetailID = Convert.ToString(rec.Emp_DetailID),  
  12.                 ProjectName = rec.ProjectName,  
  13.                 ManagerName = rec.ManagerName,  
  14.                 City = rec.City,  
  15.                 Mobile = rec.Mobile  
  16.             });  
  17.         });  
  18.     }  
  19.     return EMPDetailList;  
  20. }
To
  1. public List < EmployeeDetailDataContract > EmployeeDetails(string EMP_ID) {  
  2.     int EmpID = Convert.ToInt32(EMP_ID);  
  3.     var query = (from a in ctx.EmployeeDetails  
  4.     where a.Emp_ID == EmpID  
  5.     select a).Distinct();  
  6.   
  7.     List < EmployeeDetailDataContract > EMPDetailList = new List < EmployeeDetailDataContract > ();  
  8.     if (query.ToList().Count > 0) {  
  9.         query.ToList().ForEach(rec = > {  
  10.             EMPDetailList.Add(new EmployeeDetailDataContract {  
  11.                 Emp_ID = Convert.ToString(rec.Emp_ID),  
  12.                 Emp_DetailID = Convert.ToString(rec.Emp_DetailID),  
  13.                 ProjectName = rec.ProjectName,  
  14.                 ManagerName = rec.ManagerName,  
  15.                 City = rec.City,  
  16.                 Mobile = rec.Mobile  
  17.             });  
  18.         });  
  19.     }  
  20.     return EMPDetailList;  
  21. }
Now run your application.

Up Next
    Ebook Download
    View all
    milton
    Read by 0 people
    Download Now!
    Learn
    View all