title: Call custom function in EF LINQ query Where clause
body: <p>Env: EF6 + Code First</p>
  
  <p>I want to be able to call a custom function in the Where clause of a LINQ query</p>
  
  <p>So this line:</p>
  
  <pre><code>var activeStaff = Repo.Staff.Where(s =&gt; s.EndDate == null || s.EndDate.Value &gt; DateTime.Today);
  </code></pre>
  
  <p>becomes:</p>
  
  <pre><code>var activeStaff = Repo.Staff.Where(s =&gt; MyEdmFunctions.IsCurrentStaff(s));
  </code></pre>
  
  <p>This is what I have tried, </p>
  
  <pre><code>public class MyContext : DbContext
  {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Add(new MyCustomConvention());
      }
  }
  
  public class MyCustomConvention : IConceptualModelConvention&lt;EdmModel&gt;
  {
      public void Apply(EdmModel item, DbModel model)
      {
          var boolType = PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Boolean);
          var staffType = item.EntityTypes.Single(e =&gt; e.Name == "Staff");
  
          var payLoad = new EdmFunctionPayload
          {
              ParameterTypeSemantics = ParameterTypeSemantics.AllowImplicitConversion,
              IsComposable = true,
              IsNiladic = false,
              IsBuiltIn = false,
              IsAggregate = false,
              IsFromProviderManifest = true,
              Parameters = new[] { FunctionParameter.Create("Staff", staffType, ParameterMode.In) },
              ReturnParameters = new[] { FunctionParameter.Create("ReturnType", boolType, ParameterMode.ReturnValue) }
          };
          var function = EdmFunction.Create("IsCurrentStaff", "My.Core.Data", DataSpace.CSpace, payLoad, null);
          item.AddItem(function);
      }
  }
  
  public static class MyEdmFunctions
  {
      [DbFunction("My.Core.Data", "IsCurrentStaff")]
      public static bool IsCurrentStaff(Staff s)
      {
          return s.EndDate == null || s.EndDate &gt; DateTime.Today;
      }
  }
  </code></pre>
  
  <p>But I'm getting "Specified method is not supported." error from EntityFramework's internal CTreeGenerator class (after decompilation)</p>
  
  <pre><code>    public override DbExpression Visit(NewRecordOp op, Node n)
      {
          throw new NotSupportedException();
      }
  </code></pre>
  
  <p>Can someone please confirm if there is really no way to call a custom function in the where clause?</p>
  
  <p>I know it's possible to create a stored procedure and map it in the model. But is there a way to write it in C#?</p>
  
  <p>Thanks.</p>