I am using the following code:
from c in Country
where c.IsActive.Equals(true)
orderby c.CountryName
select new
{
countryIDCode = c.CountryID + "|" + c.TwoDigitCode,
countryName = c.CountryName
}
But I get this error while running it:
Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.
CountryID is int
type and TwoDigitCode is string
type.
How do I concatenate properly?
If this error is preventing you from progressing and is a small dataset you could could hydrate your retrieval from the database by by enumerating the query (call ToList). From that point on, your operations will be against in-memory objects and you may not encounter the error you are receiving.
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
});
Use 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
}