繁体   English   中英

linq查询无法将类型为“ System.Boolean”的对象转换为类型为“ System.String”的对象

[英]linq query getting Unable to cast object of type 'System.Boolean' to type 'System.String'

我的问题在Where子句table1.Field<string>(3) == table2.Field<string>(3) 这些值是0或1,我认为linq会将其更改为bool值。 Linq获取“无法将类型为'System.Boolean'的对象强制转换为类型为'System.String'”。

我尝试了table1[3].ToString() == table2[3].ToString() ,但是它没有出现错误或匹配行,我知道它应该包含一些内容。

我的查询:

var match = (from table1 in dt1.AsEnumerable()
                     join table2 in dt2.AsEnumerable() on table1[1].ToString() equals table2[1].ToString())
                     where table1.Field<string>(3) == table2.Field<string>(3)
                     && table1.Field<string>("ID") == table2.Field<string>("ID")
                     select table1).ToList();

我正在寻找在行上添加更多where子句的方法,但是对于那些要比较0或1值的子句却失败了。

谢谢您的帮助

因此,看起来根本的问题是table1[3]System.Stringtable2[3]System.Boolean 因此, table2.Field<string>(3)table2.Field<string>(3)上失败。

因此where使用适当的测试替换您的where

where (table1.Field<string>(3) == "1") == table2.Field<bool>(3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM