views:

207

answers:

4

Greetings, I am having some issues with using a bool operation within a Where clause extension method of an IQueryable object obtained using Linq to Entities. The first example is showing what does work using Bool1 as the operation I need to move to a where clause extension method. The second example is what doesn't work after the change. Bool1 is is completely ignored and doesn't impact the results.

Example 1:

var results =
     from a in context.aTable1
     where a.Bool1 == false && a.Bool2  == false
     select new
     {
           Column1 = a.Column1
           Bool1 = a.Bool1
           Bool2 = a.Bool2
     };

results.Where(l => l. Column1.Contains(fooString));

Example 2:

var results =
     from a in context.aTable1
     where a.Bool2  == false
     select new
     {
           Column1 = a.Column1
           Bool1 = a.Bool1
           Bool2 = a.Bool2
     };

results.Where(l => l.Bool1 == false);
results.Where(l => l. Column1.Contains(fooString));

These are over simplified examples, however I hope they show what I am trying to do. The where extension methods are in a different method and are the reason they cannot be done when I am creating the original query.

I have tried the following other ways of doing the same thing with that where clause:

results.Where(l => !l.Bool1);
results.Where(l => l.Bool1.Equals(false));

They have the same effect which is nothing.

+5  A: 

Your second and third queries are getting "lost" because you're not assigning them to anything. Try something like this instead:

var results = from a in context.aTable1
              where !a.Bool2
              select new
              {
                  Column1 = a.Column1
                  Bool1 = a.Bool1
                  Bool2 = a.Bool2
              };

results = results.Where(l => !l.Bool1);
results = results.Where(l => l.Column1.Contains(fooString));
LukeH
DOH!!! I feel stupid, I can't believe I didn't notice that I forgot to assign it. Thanks for the the help you rock!!
Rodney Foley
+1  A: 

You're not assigning the results of your queries to anything. Each call to Where() returns the results of that query, which you'll need to do something with.

In your case, you have a few options (all of which produce the same final result):

  • If you want to do something with the non-Bool1 results, you can choose to keep them around:

    var notBool1 = results.Where(l => !l.Bool1);
    var query = notBool1.Where(l => l.Column1.Contains(fooString));
    
  • You can chain Where() calls together:

    var query = results.Where(l => !l.Bool1)
                       .Where(l => l.Column1.Contains(fooString));
    
  • This is probably the fastest, although perhaps not by much:

    var query = results.Where(l => !lBool1 && l.Column1.Contains(fooString));
    
lc
A: 

How about

results.Where(l => l.Bool1 == false && l.Column1.Contains(fooString));
Stuart Dunkeld
A: 

Your .Where queries return a collection, they do not modify the collection they were called on.

var numbers = new int [] { 1,2,3,4,5,6};
numbers.Where(x => x % 2 == 0); // numbers still contains 1,2,3,4,5,6
var evens = numbers.Where(x => x % 2 == 0); // evens contains 2,4,6, numbers 1,2,3,4,5,6
numbers = numbers.Where(x => x % 2 == 0); // numbers now contains 2,4,6
Kirschstein