tags:

views:

56

answers:

2

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;
+3  A: 

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

dtb
or "where Array.IndexOf(myArray, a.Field) > -1"
Floyd
@Floyd: That works if you use LINQ-to-Objects, but I'm not sure if it does when you use LINQ-to-SQL.
dtb
@Floyd yea that works but it's ugly
msarchet
@msarchet: See mscorlib.dll - "bool IList.Contains(object value) { return Array.IndexOf(this, value) >= GetLowerBound(0); }" -This is the Framework-Implementation for Array.Contains(..). But its true, its not so beautiful as "myArray.Contains(a.Field)".
Floyd
@Floyd I understand that `myArray.Contains(a.Field)` is just an abstraction of something much more complex. I was talking more from a readability standpoint.
msarchet
A: 

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;
Øyvind Bråthen
This should work but its so slow
Floyd
Will Any really be that much slower than Contains?
Øyvind Bråthen
I think, because Contians use Array.IndexOf and this interate the array and compare with equals. Any interates the array and execute the delegeate.
Floyd
I'll check that out. Thanks Floyd.
Øyvind Bråthen