0

I want to perform the following query in Entity framework 6. Although i tried converting integer field to strings but it did not worked out.

using (var context = new HospitalEntities())
            {
                var ibed = from u in context.Employee
                           where u.EmployeeId == employeeId
                               select new
                               {
                                   _empId = u.EmployeeId,
                                   _name =Convert.ToString(u.Code) + u.EmployeeName


                               };

Model Class:

public class Employee
{

    public int EmployeeId { get; set; }
    public int Code { get; set; }
    public string EmployeeName { get; set; }

}

Please help me. Thanks in advance.

2 Answers 2

2

In EF 6 you need to use System.Data.Entity.SqlServer.SqlFunctions (NOT System.Data.Objects.SqlClient.SqlFunctions which was used in prior versions)

using (var context = new HospitalEntities())
{
var ibed = from u in context.Employee
where u.EmployeeId == employeeId
select new
{
      _empId = u.EmployeeId,
      _name = System.Data.Entity.SqlServer.SqlFunctions.StringConvert((int?) u.Code) + u.EmployeeName


};

I'm using the long name here for emphasis. In prior projects you'll just replace the includes. Again, this is for Entity Framework 6 when trying to do StringConvert or other SQL functions.

//using System.Data.Objects.SqlClient;
using System.Data.Entity.SqlServer;
Sign up to request clarification or add additional context in comments.

1 Comment

As of EF 6.1 (blogs.msdn.com/b/adonet/archive/2014/03/17/…) you can now concat any primitive type.. e.g entity.MyInt + entity.MyStr + "hello" and it will work.
0

Use System.Data.Objects.SqlClient.SqlFunctions.StringConvert

_name =SqlFunctions.StringConvert((double)u.Code) + u.EmployeeName

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.