我有这个LINQ语句 Dim Demo = From d In DBDataTable.AsEnumerable _ Select id = d.Field(Of Integer)("id"), _ Column = d.Field(Of Object) (_column2), _ Col3 = d.Field(Of Object)(_column3), _ Col4 = IIf(_Col4 -1, d.Field(Of Object)(_Col4),
Dim Demo = From d In DBDataTable.AsEnumerable _ Select id = d.Field(Of Integer)("id"), _ Column = d.Field(Of Object) (_column2), _ Col3 = d.Field(Of Object)(_column3), _ Col4 = IIf(_Col4 <> -1, d.Field(Of Object)(_Col4), Nothing)
如果/ iif在select中,我有什么方法可以使用吗?
[这是对问题评论中讨论的总结.]您的代码将无法工作,因为IIf始终会评估true和false部分.因此,如果_Col4 = -1,则评估d.Field(Of Object)(_ Col4)将引发异常.
相反,使用If(condition,true,false),它的作用就像C#的条件一样? true:false运算符,仅根据条件计算true或false部分.所以,你的代码应该是:
Dim Demo = From d In DBDataTable.AsEnumerable _ Select id = d.Field(Of Integer)("id"), _ Col2 = d.Field(Of Object)(_column2), _ Col3 = d.Field(Of Object)(_column3), _ Col4 = If(_Col4 <> -1, d.Field(Of Object)(_Col4), Nothing)