Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

DbComparisonExpression requires arguments with comparable types

Article:
Viewed:  1751 
Posted On:  26/05/2015 20:08:50 
While working Entity Framework I got this problem/ 

 

While working with Entity Framework I got below error:

1.png

Image 1.

Basically what happen when you work with Entity Framework: EF tries to translate your query into SQL, and while doing that it doesn't seem to take into account your type conversion operators. Simply use this query instead:

Change your Code:

From:

public List<EmployeeDetailDataContract> EmployeeDetails(string EMP_ID)
        {           
            var query = (from a in ctx.EmployeeDetails
                         where a.Emp_ID.Equals(EMP_ID)
                         select a).Distinct();
 
            List<EmployeeDetailDataContract> EMPDetailList = new List<EmployeeDetailDataContract>();
            if (query.ToList().Count > 0)
            {
                query.ToList().ForEach(rec =>
                {
                    EMPDetailList.Add(new EmployeeDetailDataContract
                    {
                        Emp_ID = Convert.ToString(rec.Emp_ID),
                        Emp_DetailID = Convert.ToString(rec.Emp_DetailID),
                        ProjectName = rec.ProjectName,
                        ManagerName = rec.ManagerName,
                        City = rec.City,
                        Mobile = rec.Mobile
                    });
                });
            }
            return EMPDetailList;
        }


To:


public List<EmployeeDetailDataContract> EmployeeDetails(string EMP_ID)
{
            int EmpID = Convert.ToInt32(EMP_ID);
            var query = (from a in ctx.EmployeeDetails
                         where a.Emp_ID == EmpID
                         select a).Distinct();
 
            List<EmployeeDetailDataContract> EMPDetailList = new List<EmployeeDetailDataContract>();
            if (query.ToList().Count > 0)
            {
                query.ToList().ForEach(rec =>
                {
                    EMPDetailList.Add(new EmployeeDetailDataContract
                    {
                        Emp_ID = Convert.ToString(rec.Emp_ID),
                        Emp_DetailID = Convert.ToString(rec.Emp_DetailID),
                        ProjectName = rec.ProjectName,
                        ManagerName = rec.ManagerName,
                        City = rec.City,
                        Mobile = rec.Mobile
                    });
                });
            }
            return EMPDetailList;
        }

Now Run Your Application:

2.png

Image 2.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us