title: How to Refactor LINQ Query to Use Delegate or Expression<Func<T, bool>>
body: <p>This method works great and generates only 1 query (utilizing .Includes() to eager load).
  However, I would like to refactor the AbleToDelete code to be more reusable.
  (Using EF4)</p>
  
  <pre><code>        public override IEnumerable&lt;AffectProjection&gt; SelectAll(SearchAffects page)
          {
              IQueryable&lt;Affect&gt; query = BuildSearchQuery(page);
  
              IEnumerable&lt;AffectProjection&gt; results = query.Select(entity =&gt; new AffectProjection()
              {
                  AffectID = entity.AffectID,
                  AffectCode = entity.AffectCode,
                  AffectName = entity.AffectName,
  
                  AbleToDelete = !((entity.Foo1s.Any(pa =&gt; !pa.Inactive))
                                  || (entity.Foo2s.Any(ea =&gt; !ea.Inactive))
                                  || (entity.Foo3s.Any(sa =&gt; !sa.Inactive))
                                  || (entity.Foo4s.Any(spa =&gt; !spa.Inactive)))
              }).ToArray();
  
              return results;
          }
  </code></pre>
  
  <p>I moved the code into an Expression Func structure but can't figure out how to replace my code that is setting the AbleToDelete property.  Advise?</p>
  
  <pre><code>public Expression&lt;Func&lt;Affect, bool&gt;&gt; DelegateExpression = entity =&gt;
              !((entity.Foo1s.Any(pa =&gt; !pa.Inactive))
                  || (entity.Foo2s.Any(ea =&gt; !ea.Inactive))
                  || (entity.Foo3s.Any(sa =&gt; !sa.Inactive))
                  || (entity.Foo4s.Any(spa =&gt; !spa.Inactive)));
  </code></pre>
  
  <p>Last but not least, this solution compiles but fails at runtime with error: "NotSupportedException - The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."  Trying to use a delegate which isn't supported:</p>
  
  <pre><code>        public override IEnumerable&lt;AffectProjection&gt; SelectAll(SearchAffects page)
          {
              IQueryable&lt;Affect&gt; query = BuildSearchQuery(page);
  
              //Create delegate instance and register method -- single statement
              var deleteAbilityAllowed = new CanDelete(GetAbleToDelete);
  
              IEnumerable&lt;AffectProjection&gt; results = query.Select(entity =&gt; new AffectProjection()
              {
                  AffectID = entity.AffectID,
                  AffectCode = entity.AffectCode,
                  AffectName = entity.AffectName,
  
                  AbleToDelete = deleteAbilityAllowed(entity)
              }).ToArray();
  
              return results;
          }
  
          public delegate bool CanDelete(Affect entity);
  
          public bool GetAbleToDelete(Affect entity)
          {
              return !((entity.Foo1s.Any(pa =&gt; !pa.Inactive))
                  || (entity.Foo2s.Any(ea =&gt; !ea.Inactive))
                  || (entity.Foo3s.Any(sa =&gt; !sa.Inactive))
                  || (entity.Foo4s.Any(spa =&gt; !spa.Inactive)));
          }
  </code></pre>
  
  <p>Please advise and thank you in advance!</p>