Jaan
Jaan

Reputation: 3393

How to concatenate strings in Entity Framework Query

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.

Upvotes: 0

Views: 4047

Answers (2)

Greg
Greg

Reputation: 2600

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;

Upvotes: 2

Cris
Cris

Reputation: 13351

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

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

Upvotes: 0

Related Questions