Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 具有不同字段的LINQ where子句_Vb.net_Linq_Where - Fatal编程技术网

Vb.net 具有不同字段的LINQ 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=

我试图根据函数接收到的布尔值更改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=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)
请参阅上的注释以了解更多信息