Wah*_*eed 11 linq linq-to-entities entity-framework
我使用以下代码:
from c in Country
where c.IsActive.Equals(true)
orderby c.CountryName
select new
{
countryIDCode = c.CountryID + "|" + c.TwoDigitCode,
countryName = c.CountryName
}
Run Code Online (Sandbox Code Playgroud)
但是我在运行它时遇到了这个错误:
无法将类型"System.Int32"强制转换为"System.Object"类型.LINQ to Entities仅支持转换实体数据模型基元类型.
CountryID是int类型,TwoDigitCode是string类型.
我如何正确连接?
小智 23
使用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
}
Run Code Online (Sandbox Code Playgroud)
如果此错误阻止您进行并且是一个小型数据集,则可以通过枚举查询(调用ToList)来保存从数据库中检索的内容.从那时起,您的操作将针对内存中的对象,您可能不会遇到您收到的错误.
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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17993 次 |
| 最近记录: |