Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:59:59+00:00 2026-05-16T22:59:59+00:00

I would like to create a compiled query which uses reusable where predicates. An

  • 0

I would like to create a compiled query which uses reusable where predicates. An example to make this clear:

ObjectContext.Employees.Where(EmployeePredicates.CustomerPredicate)

EmployeePredicates is a static class with a property CustomerPredicate that looks like this:

public static Expression<Func<Employee, bool>> CustomerPredicate
    {
        get
        {
            return t => t.CustomerId == 1;
        }
    }

This works as expected.
In most cases however, you would like to pass a parameter to Expression. To achieve this I have to change the property to a static function:

 public static Expression<Func<Employee, bool>> CustomerPredicate(int id)
    {
         return t => t.CustomerId == id;
    }

And I can use this like this:

ObjectContext.Employees.Where(EmployeePredicates.CustomerPredicate(id))

This works, but now comes the tricky part. I would like to compile this query… Visual studio doesn’t give me any compile errors, but when I run this example the following exception is thrown at runtime:

Internal .NET Framework Data Provider error 1025

Just so we’re on the same page here is the full code that gives me the exception:

var _compiledQuery = CompiledQuery.Compile<AdventureWorksEntities, int, IQueryable<Employee>>(
                    (ctx, id) =>
                            (ctx.Employee.Where(EmployeePredicates.CustomerPredicate(id))
                        ));

Does anyone have a clue why this exception is being thrown? I’ve taken this way of working from http://www.albahari.com/nutshell/linqkit.aspx. Any help would be much appreciated.


Thanks Jon,

The problem with the approach you describe, is that chaining predicates together will become very hard. What if I need to combine this predicate with another predicate that filters the employees with a certain name? Then you end up with a lot of selects to pass in the parameters.

    (ctx, id, name) => 
(ctx.Employee.Select(emp => new {emp, id})
.Where(EmployeePredicates.CustomerPredicate(id))
.Select(emp => new {emp, name})
.Where(EmployeePredicates.NamePredicate(name))

It gets even worse when you’re working on joined tables.

 (ctx, id, name) => 
    (ctx.Employee
     .Join(ctx.Contact, e=> e.ContactId, c => c.Id), (emp, cont) => new Container<Employee, Customer> {Employee = emp, Contact = cont})
    .Where(EmployeePredicates.CustomerPredicate(id))
    .Where(EmployeePredicates.NamePredicate(name))
    .Select(t => new EmployeeDTO {Name = t.cont.Name, Customer = e.emp.Customer})

Because each Where() operates on something of type T and returns something of type T, the WherePredicates in the code above must work on the type Container. This makes it very hard to reuse the Predicates. And reuse was the initial goal of this approach…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:00:00+00:00Added an answer on May 16, 2026 at 11:00 pm

    The problem is that the Entity Framework is trying to examine the expression tree represented by

     (ctx, id) => (ctx.Employee.Where(EmployeePredicates.CustomerPredicate(id))
    

    It can’t do that, because it doesn’t know what EmployeePredicates.CustomerPredicate does.

    As for the best fix… I’m not sure. Basically it’s got to know at query compile time what the full query looks like, just with the placeholders for parameters.

    I suspect the best solution will involve something like this:

    public static Expression<Func<Employee, int, bool>> CustomerPredicate()
    {
         return (t, id) => t.CustomerId == id;
    }
    

    … as that raises the abstraction by one level; it gives you an expression tree which uses id as a ParameterExpression, which is something you’ll need in order to build the appropriate expression tree to call CompileQuery. It gets a little hard to think about, unfortunately 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like create a array of structure which have a dynamic array :
I would like to create this shape using just css. I am pretty sure
I would like to create an application which can learn to classify a sequence
I would like to create a Makefile which also creates a simple script for
I would like to create a reusable interface to transfer some proprietary information over
I would like to create a c++ type that mimic the build-in type exactly.
I would like to create a json object to send as a post array,
I would like to create set of strings and below is the only limitation.
I would like to create a class that runs something (a runnable) at regular
I would like to create a virtual host in apache2, but I want it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.