In LINQ to Entities, concatenate int and string.

entity-framework linq linq-to-entities

Question

I'm using the code below:

from c in Country
 where c.IsActive.Equals(true)
 orderby c.CountryName
 select new
 {
     countryIDCode = c.CountryID + "|" + c.TwoDigitCode,
     countryName = c.CountryName
 }

But when I use it, I get this error:

Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

StateID isint two-digit code type isstring type.

How do I correctly concatenate?

1
11
6/24/2015 9:25:14 PM

Accepted Answer

If this problem is keeping you from moving forward and the dataset is modest, you might hydrate your database retrieval by enumerating the query (call ToList). Your actions will then be against in-memory objects, and you may not run into the issue you are now experiencing.

var countries = (from c in Country
where c.IsActive.Equals(true)
orderby c.CountryName
select c).ToList();


var countryCodes = (from c in countries
where c.IsActive.Equals(true)
    orderby c.CountryName
    select new
    {
        countryIDCode = c.CountryID + "|" + c.TwoDigitCode,
        countryName = c.CountryName
    });
7
4/21/2010 12:06:50 PM

Popular Answer

Implement System.Data.Objects. SqlClient.SqlFunctions.StringConvert

 from c in Country
 where c.IsActive.Equals(true)
 orderby c.CountryName
 select new
 {
     countryIDCode = SqlFunctions.StringConvert((double)c.CountryID) 
                     + "|" + c.TwoDigitCode,
     countryName = c.CountryName
 }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow