English 中文(简体)
Linq, Where Extension Method, Lambda Expressions, and Bool s
原标题:

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.

最佳回答

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));
问题回答

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));
    

How about

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

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签