none
Problem trying to concat Int and String

    Question

  • Hi all,

    I'm trying to concat a string and an integer in a query using the following code:

    var proyecto= from p in ....
    ........

    select new
    {
    AislamientoAcusticoMuro =String.Concat(aislamientosAcusticoMuro.AislamientoAcusticoMuro,  " + ")
    }

    and I get this error:

    The 'System.String Concat(System.Object, System.Object)' method is not recognized by LINQ to Entities, and cannot be translated into a store expression.


    aislamientosAcusticoMuro.AislamientoAcusticoMuro is an integer.

    I've also tried

    String.Concat(aislamientosAcusticoMuro.AislamientoAcusticoMuro.ToString(),  " + ")

    and I got another error:

    The 'System.String ToString()' method is not recognized by LINQ to Entities, and cannot be translated into a store expression.

    Convert.ToString didnt work either.

    Any suggestion about it?
    Monday, April 28, 2008 4:41 PM

Answers

  • Ryan is right that LINQ to Entities doesn't currently support either the ToString() method or an overload of String.Concatenate() that takes anything but strings.

     

    However, rather than using ToList() or ToArray(), that will materialize the whole results first before you do conversion, in most cases I would recommend to use AsEnumerable(), that will switch the processing of the projection to the client side (using LINQ to Objects), without loosing the streaming effect.

     

    In your sample that would look like:

     

    Code Snippet

    var proyecto= from p in ....
        ........
        select aislamientosAcusticoMuro.AislamientoAcusticoMuro;

    var proyectoClientQuery = from aislamiento in projecto.AsEnumerable()

        select new {AislamientoAcusticoMuro =String.Concat(aislamiento, " + ")};

     

     

    If you need this type of conversion to string in other part of the query than the top-level projection, then this technique cannot help. In that case I would recommend to resort to an EntitySQL query.

     

    Hope this helps,

    -Diego

     

    Monday, April 28, 2008 11:22 PM

All replies

  • That error means LINQ to Entities doesn't know how to generate SQL for that method call.  You could materialize the query first (call ToList(), ToArray(), First() or something else that causes the query to be executed), so the data is in memory, and then do the concatination afterward.

    Monday, April 28, 2008 8:11 PM
  • Ryan is right that LINQ to Entities doesn't currently support either the ToString() method or an overload of String.Concatenate() that takes anything but strings.

     

    However, rather than using ToList() or ToArray(), that will materialize the whole results first before you do conversion, in most cases I would recommend to use AsEnumerable(), that will switch the processing of the projection to the client side (using LINQ to Objects), without loosing the streaming effect.

     

    In your sample that would look like:

     

    Code Snippet

    var proyecto= from p in ....
        ........
        select aislamientosAcusticoMuro.AislamientoAcusticoMuro;

    var proyectoClientQuery = from aislamiento in projecto.AsEnumerable()

        select new {AislamientoAcusticoMuro =String.Concat(aislamiento, " + ")};

     

     

    If you need this type of conversion to string in other part of the query than the top-level projection, then this technique cannot help. In that case I would recommend to resort to an EntitySQL query.

     

    Hope this helps,

    -Diego

     

    Monday, April 28, 2008 11:22 PM