views:

274

answers:

1

Maybe a simple question, I'm trying to get a result from a table where the Name column contains all of an array of search terms. I'm creating a query and looping through my search strings, each time assigning the query = query.Where(...);. It appears that only the last term is being used, I supposed because I am attempting to restrict the same field each time. If I call .ToArray().AsQueryable() with each iteration I can get the cumlative restrinction behavior I'm looking for, but it there an easy way to do this using defered operators only?

Thanks!

+2  A: 

If you're doing something like:

foreach (int foo in myFooArray)
{
  query = query.where(x => x.foo == foo);
}

...then it will only use the last one since each where criteria will contain a reference to the 'foo' loop variable.

If this is what you're doing, change it to:

foreach (int foo in myFooArray)
{
  int localFoo = foo;
  query = query.where(x => x.foo == localFoo);
}

...and everything should be fine again.

If this is not what is happening, please provide a code sample of what you're doing...

KristoferA - Huagati.com
Good catch, must be it.
Marc
Ah, Perfect! Hadn't even crossed my mind. Good catch indeed...
Paul
It is one of those odd edge cases where it is not directly apparent what is going on... :) ...just because the code is not executed 'inline' like we're used to with most other C# or vb code...
KristoferA - Huagati.com