Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将LINQ中的int和string连接到实体_Linq_Entity Framework_Linq To Entities - Fatal编程技术网

将LINQ中的int和string连接到实体

将LINQ中的int和string连接到实体,linq,entity-framework,linq-to-entities,Linq,Entity Framework,Linq To Entities,我正在使用以下代码: from c in Country where c.IsActive.Equals(true) orderby c.CountryName select new { countryIDCode = c.CountryID + "|" + c.TwoDigitCode, countryName = c.CountryName } l.StateName + " " + SqlFunctions.StringConvert( (double?)l

我正在使用以下代码:

from c in Country
 where c.IsActive.Equals(true)
 orderby c.CountryName
 select new
 {
     countryIDCode = c.CountryID + "|" + c.TwoDigitCode,
     countryName = c.CountryName
 }
l.StateName + " " + SqlFunctions.StringConvert( (double?)l.Zip ).Trim()
但我在运行时遇到了以下错误:

无法将类型“System.Int32”强制转换为类型“System.Object”。LINQ to Entities仅支持转换实体数据模型基元类型

CountryID是
int
类型,TwoDigitCode是
string
类型


如何正确连接?

使用
c.CountryId.ToString()
获取CountryId的字符串表示形式,并将其连接到TwoDigitCode字段:

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

我希望您的错误实际上是一个编译器错误(想不到编译器会以任何方式允许此查询。但我不确定它如何知道Linq to Entities部分)。

如果需要将它们作为字符串连接在一起,可以使用以下方法:


本主题包含可转换为命令树规范函数并在服务器上执行的CLR方法列表:


对于不在此列表中的CLR方法,您必须使用.AsEnumerable()将结果下拉到客户端,并执行LINQ to Objects查询。

这是旧版本实体框架的限制。我认为v4解决了这个问题。对于您的版本,解决方法是将结果转换为可枚举:

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

如果此错误阻止您继续,并且是一个小数据集,您可以通过枚举查询(调用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
    });
int-to-string转换没有任何限制

因此,只需在两个不同的列中返回Int和字符串,然后使用
aseneumerable
方法将它们连接到.NET中:

var cListTemp = from c in Country
    where c.IsActive.Equals(true)
    orderby c.CountryName
    select new
    {
        countryID = c.CountryID,
        twoDigitCode = c.TwoDigitCode,
        countryName = c.CountryName
    };

var cList = cListTemp.AsEnumerable().Select(c => new {
    countryIDCode = c.countryID + "|" + c.twoDigitCode,
    countryName = c.countryName
});

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

我能够用以下代码连接字符串:

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

似乎只要
SqlFunctions.StringConvert((double?)l.Zip)
就足够了,但是结果字符串的左边有一堆填充,这会导致字符串比较不匹配。事实证明,
Trim()
可以去除多余的部分。我相信
SqlFunctions.StringConvert((double?)l.Zip).Trim()
可以有效地转换为SQL:
LTrim(RTrim(STR(Zip)))

不,这不能使用。在这种情况下出现以下错误:LINQ to Entities无法识别“System.String ToString()”方法,并且无法将此方法转换为存储表达式。sql客户端上无法执行..ToString()和.Format(),因此这将产生运行时错误。在L2S中工作正常,您可能正在使用EF。但是,没有理由拒绝投票,您应该更明确。没有,它会给出运行时错误:LINQ to Entities无法识别方法“System.String Format(System.String,System.Object,System.Object)”方法,并且此方法无法转换为存储表达式..ToString()和.Format()无法在sql客户端上执行,因此这将产生运行时错误。问题是“在LINQ中连接int和string”,而不是在LINQ中连接到sql等。OP应该更清楚。林克!=SQLOP也错误地为问题添加了标签。+1用于回答OP提出的问题,即使这不是他真正想要的问题。我正在尝试您的方法,但遇到编译时错误:查询体必须以select子句或group子句结尾。您显示的查询在EF 1和EF 4中或多或少都能正常工作。是的,但我不确定的是,在v4中您是否仍然需要这种解决方法,或者限制已经解决。使用EF4时,会出现相同的错误。找到您的答案并使用AsEnumerable()来解决它。现在真的更喜欢L2而不是EF4……我不太确定。从我的观点来看,数据库类型的行为总是有点奇怪,而SqlFunctions实用程序正是我用来解决问题的工具,就像我所发布的一样。我喜欢这种方法,而不是像ToList转换这样不太明显的解决方案,因为它明确地向维护代码的任何人显示了您正在做什么。我相信这应该是公认的答案,因为它解决了问题,而没有像Nicholas Murray的答案那样引入性能惩罚(尽管它确实是说
…而且是一个小数据集
)。在EF6中,这被移动到System.Data.Entity.SqlServer.SqlFunctions.StringConvert,我认为这个答案已经过时,而且总体上是一个糟糕的做法。这是一个快速的“脏”修复,目前有效,但以后会咬到你(例如,当您当前的小数据集突然增长,或者客户决定在另一个更大的数据集上使用完全相同的内容时)。请参阅flyfisher1952的答案,该答案直接使用db函数,使其成为比此答案更好的解决问题的方法。