gpt4 book ai didi

linq - 在 LINQ to Entities 中连接 int 和 string

转载 作者:行者123 更新时间:2023-12-02 11:52:12 25 4
gpt4 key购买 nike

我正在使用以下代码:

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

但是我在运行时遇到此错误:

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

CountryID为int类型,TwoDigitCode为string类型。

如何正确连接?

最佳答案

使用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
}

关于linq - 在 LINQ to Entities 中连接 int 和 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2682264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com