Vb.net 具有不同字段的LINQ where子句
我试图根据函数接收到的布尔值更改Where子句将应用于哪个字段。到目前为止,我有:Vb.net 具有不同字段的LINQ where子句,vb.net,linq,where,Vb.net,Linq,Where,我试图根据函数接收到的布尔值更改Where子句将应用于哪个字段。到目前为止,我有: Public Function SomeFunction(ByVal value As String, Optional bValueIsString As Boolean = False) As JsonResult ... Dim q = (From t In table Where If(bValueIsString, t.Desc=value, t.Id=
Public Function SomeFunction(ByVal value As String, Optional bValueIsString As Boolean = False) As JsonResult
...
Dim q = (From t In table
Where If(bValueIsString, t.Desc=value, t.Id=value)
Select New With {.Id,
.LongDescription,
.Desc)})
...
End Function
但我一直收到:
Conversion from string "someString" to type 'Integer' is not valid.
。。当我将bValueIsString设置为true时
-
我知道ScottGu的动态LINQ库,但如果可能的话,我宁愿不使用任何外部组件。这是因为if函数的操作数2和3必须具有相同的类型,或者可以隐式转换,以便可以自动转换。在这种情况下,操作数将被转换为整数,
value
不包含有效的转换值。将代码更改为显式比较字符串可以解决此问题:
If(bValueIsString, t.Desc=value, t.Id.ToString()=value)
请参阅上的注释以了解更多信息