Cómo concatenar cadenas en 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


                               };

Clase de modelo:

public class Employee
{

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

}

Por favor, ayúdame. Gracias por adelantado.

preguntado el 27 de noviembre de 13 a las 07:11

2 Respuestas

In SI 6 necesitas usar 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 al intentar hacer StringConvert or other SQL functions.

//using System.Data.Objects.SqlClient;
using System.Data.Entity.SqlServer;

respondido 06 mar '14, 22:03

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. - Roger Johansson

Utilice la herramienta System.Data.Objects.SqlClient.SqlFunctions.StringConvert

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

respondido 27 nov., 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.