title: EntitySet<T>.Where(myPredicate) throws NotSupportedException
body: <p><strong>EDIT</strong>: Let's try this again.  This time I've used the AdventureWorks sample database so you can all play along.  This will rule out anything crazy I've done in my own database.  Here's a new example demonstrating what works and what I would expect to work (but doesn't).  Can anyone explain why it doesn't work or suggest a different way of achieving my goal (refactoring out the common expression so it can be reused elsewhere)?</p>
  
  <pre><code>using (AdventureWorksDataContext db = new AdventureWorksDataContext())
  {
      // For simplicity's sake we'll just grab the first result.
      // The result should have the name of the SubCategory and an array of Products with ListPrice greater than zero.
      var result = db.ProductSubcategories.Select(subCategory =&gt; new
      {
          Name = subCategory.Name,
          ProductArray = subCategory.Products.Where(product =&gt; product.ListPrice &gt; 0).ToArray()
      }).First();
      Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice &gt; 0.", result.ProductArray.Length, result.Name);
      // Output should say: There are 3 products in SubCategory Bib-Shorts with ListPrice &gt; 0.
  
      // This won't work.  I want to pull the expression out so that I can reuse it in several other places.
      Expression&lt;Func&lt;Product, bool&gt;&gt; expression = product =&gt; product.ListPrice &gt; 0;
      result = db.ProductSubcategories.Select(subCategory =&gt; new
      {
          Name = subCategory.Name,
          ProductArray = subCategory.Products.Where(expression).ToArray() // This won't compile because Products is an EntitySet&lt;Product&gt; and that doesn't have an overload of Where that accepts an Expression.
      }).First();
      Console.WriteLine("There are {0} products in SubCategory {1} with ListPrice &gt; 0.", result.ProductArray.Length, result.Name);
  }
  </code></pre>
  
  <p><strong><code>&lt;/Edit&gt;</code></strong></p>
  
  <p>The following LINQ to SQL works fine:</p>
  
  <pre><code>var result = from subAccount in db.SubAccounts
               select new ServiceTicket
               {
                   MaintenancePlans = subAccount.Maintenances.Where(plan =&gt; plan.CancelDate == null &amp;&amp; plan.UpgradeDate == null).Select(plan =&gt; plan.ToString()).ToArray()
                   // Set other properties...
               };
  </code></pre>
  
  <p>However, I want to break out the predicate passed to the <code>Where</code> since it's used throughout the code.  But if I try and pass a defined predicate into the <code>Where</code> it fails, such as:</p>
  
  <pre><code>Func&lt;DatabaseAccess.Maintenance, bool&gt; activePlanPredicate = plan =&gt; plan.CancelDate == null &amp;&amp; plan.UpgradeDate == null;
  var result = from subAccount in db.SubAccounts
               select new ServiceTicket
               {
                   MaintenancePlans = subAccount.Maintenances.Where(activePlanPredicate).Select(plan =&gt; plan.ToString()).ToArray()
                   // Set other properties...
               };
  </code></pre>
  
  <p>This makes no sense to me.  Can anyone explain what's going on?  <code>Maintenances</code> is of type <code>EntitySet&lt;DatabaseAccess.Maintenance&gt;</code>.  The error I get is: </p>
  
  <blockquote>
    <p>System.NotSupportedException:
    Unsupported overload used for query
    operator 'Where'..</p>
  </blockquote>
  
  <p><strong>EDIT</strong>: For those interested, here's what Reflector has for the first (working) example with Optimization set to .NET 2.0:</p>
  
  <pre><code>using (BugsDatabaseDataContext db = new BugsDatabaseDataContext())
  {
      ParameterExpression CS$0$0001;
      ParameterExpression CS$0$0006;
      ParameterExpression CS$0$0010;
      return db.SubAccounts.Select&lt;SubAccount, ServiceTicket&gt;(Expression.Lambda&lt;Func&lt;SubAccount, ServiceTicket&gt;&gt;(
          Expression.MemberInit(
              Expression.New(
                  (ConstructorInfo) methodof(ServiceTicket..ctor), 
                  new Expression[0]), 
                  new MemberBinding[] 
                  { 
                      Expression.Bind(
                          (MethodInfo) methodof(ServiceTicket.set_MaintenancePlans), 
                          Expression.Call(
                              null, 
                              (MethodInfo) methodof(Enumerable.ToArray), 
                              new Expression[] 
                              { 
                                  Expression.Call(
                                      null, 
                                      (MethodInfo) methodof(Enumerable.Select), 
                                      new Expression[] 
                                      { 
                                          Expression.Call(
                                              null, 
                                              (MethodInfo) methodof(Enumerable.Where), 
                                              new Expression[] 
                                              { 
                                                  Expression.Property(CS$0$0001 = Expression.Parameter(typeof(SubAccount), "subAccount"), (MethodInfo) methodof(SubAccount.get_Maintenances)), 
                                                  Expression.Lambda&lt;Func&lt;Maintenance, bool&gt;&gt;(
                                                      Expression.AndAlso(
                                                          Expression.Equal(
                                                              Expression.Property(CS$0$0006 = Expression.Parameter(typeof(Maintenance), "plan"), (MethodInfo) methodof(Maintenance.get_CancelDate)), 
                                                              Expression.Convert(Expression.Constant(null, typeof(DateTime?)), typeof(DateTime?)), false, (MethodInfo) methodof(DateTime.op_Equality)
                                                          ), 
                                                          Expression.Equal(
                                                              Expression.Property(CS$0$0006, (MethodInfo) methodof(Maintenance.get_UpgradeDate)), 
                                                              Expression.Convert(Expression.Constant(null, typeof(DateTime?)), typeof(DateTime?)), false, (MethodInfo) methodof(DateTime.op_Equality)
                                                          )
                                                      ), 
                                                      new ParameterExpression[] { CS$0$0006 }
                                                  ) 
                                              }
                                          ), 
                                          Expression.Lambda&lt;Func&lt;Maintenance, string&gt;&gt;(
                                              Expression.Call(
                                                  CS$0$0010 = Expression.Parameter(typeof(Maintenance), "plan"), 
                                                  (MethodInfo) methodof(object.ToString), 
                                                  new Expression[0]
                                              ), 
                                              new ParameterExpression[] { CS$0$0010 }
                                          ) 
                                      }
                                  ) 
                              }
                          )
                      )
                  }
              ), 
              new ParameterExpression[] { CS$0$0001 }
          )
      ).ToList&lt;ServiceTicket&gt;();
  }
  </code></pre>
  
  <p><strong>EDIT</strong>: The Reflector output for the second example (using a predicate) is mostly similar.  The biggest difference being that, in the call to <code>Enumerable.Where</code>, rather than passing an <code>Expression.Lambda</code> it passes <code>Expression.Constant(activePlanPredicate)</code>.</p>