Linq查询字段是否在数组中?

时间:2021-09-04 21:20:29

The code below is incorrect but the idea here is that I want to grab values from "SqlTable" where the value for "Field" is inside of "Array[]".

下面的代码是不正确的,但是这里的想法是,我想从“SqlTable”中获取值,其中“字段”的值在“数组[]”中。

var Result =
    from a in SqlTable
    where a.Field is in Array[]
    select a;

2 个解决方案

#1


6  

You should be able to use the Queryable.Contains Extension Method:

您应该能够使用Queryable。包含扩展方法:

var result =
    from a in mySqlTable
    where myArray.Contains(a.Field)
    select a;

See also: Creating IN Queries With Linq To Sql

参见:使用Linq到Sql创建查询

#2


0  

I'm assuming now that Field and Array[] contains values that has a equality operator in place, and that the A. Then you can write it like this:

我假设字段和数组[]包含具有相等运算符的值,a可以这样写:

var Result =
    from a in SqlTable
    where Array[].Any( ae => ae == a.Field)
    select a;

#1


6  

You should be able to use the Queryable.Contains Extension Method:

您应该能够使用Queryable。包含扩展方法:

var result =
    from a in mySqlTable
    where myArray.Contains(a.Field)
    select a;

See also: Creating IN Queries With Linq To Sql

参见:使用Linq到Sql创建查询

#2


0  

I'm assuming now that Field and Array[] contains values that has a equality operator in place, and that the A. Then you can write it like this:

我假设字段和数组[]包含具有相等运算符的值,a可以这样写:

var Result =
    from a in SqlTable
    where Array[].Any( ae => ae == a.Field)
    select a;