sooprise
sooprise

Reputation: 23187

Linq Query If Field Is In An Array?

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[]".

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

Upvotes: 2

Views: 4768

Answers (2)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60694

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:

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

Upvotes: 0

dtb
dtb

Reputation: 217263

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

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

See also: Creating IN Queries With Linq To Sql

Upvotes: 6

Related Questions