Linq无法投出问题

bd5*_*528 5 c# linq casting

当我试图跑

InvTotal = g.Sum(d => d.Field<double>("Total")) < 0 ? "W" : "N",我得到了

Unable to cast object of type 'System.Double' to type 'System.String' 错误.

如何成功编译它需要更改代码.

Maa*_*ten 10

我认为你需要正确的括号.

var InvTotal = (g.Sum(d => d.Field<double>("Total")) < 0) ? "W" : "N"
Run Code Online (Sandbox Code Playgroud)

如果没有它们,编译器将0 ? "W" : "N"首先编译,其结果将用于比较.

有时,C#编译器需要一些帮助,如果涉及到?运营商.