I have this LINQ statement
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)
Is there any way that I can use if/iif within select ?
[This is a summary of the discussion in the question comments.]
Your code won’t work since
IIfalways evaluates both the true and the false part. Thus, evaluatingd.Field(Of Object)(_Col4)will raise an exception if_Col4 = -1.Instead, use
If(condition, true, false), which works like C#’scondition ? true : falseoperator and only evaluates either the true or the false part, depending on condition. So, your code should read: