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 8647171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:59:51+00:00 2026-06-12T12:59:51+00:00

I am trying to write a really generic way to load EF entities in

  • 0

I am trying to write a really generic way to load EF entities in batches, using the Contains method to generate a SQL IN statement. I’ve got it working if I pass the entire expression in, but when I try to build the expression dynamically, I am getting a “The LINQ expression node type ‘Invoke’ is not supported in LINQ to Entities.” So I know this means that EF thinks I’m calling an arbitrary method and it can’t translate it into SQL, but I can’t figure out how to get it to understand the underlying expression.

So If I do something like this (just showing the relevant snippets):

Function declaration:

public static List<T> Load<T>(IQueryable<T> entityQuery, int[] entityIds, Func<T, int> entityKey, int batchSize = 500, Func<T, bool> postFilter = null) where T : EntityObject
{
    var retList = new List<T>();

    // Append a where clause to the query passed in, that will use a Contains expression, which generates a SQL IN statement.  So our SQL looks something like
    // WHERE [ItemTypeId] IN (1921,1920,1922)
    // See http://rogeralsing.com/2009/05/21/entity-framework-4-where-entity-id-in-array/ for details
    Func<int[], Expression<Func<T, bool>>> containsExpression = (entityArray => (expr => entityArray.Contains(entityKey(expr))));

    // Build a new query with the current batch of IDs to retrieve and add it to the list we are returning
    newQuery = entityQuery.Where<T>(containsExpression(entityIds));
    retList.AddRange(newQuery.ToList());

    return retList;
} 

Call function:

var entities = BatchEntity.Load<ItemType>(from eItemType in dal.Context.InstanceContainer.ItemTypes
select eItemType
, itemTypeData
, (ek => ek.ItemTypeId)
);

I get “The LINQ expression node type ‘Invoke’ is not supported in LINQ to Entities.”

But if I change it to be this:

Function declaration:

public static List<T> Load<T>(IQueryable<T> entityQuery, int[] entityIds, Func<int[], Expression<Func<T, bool>>> containsExpression, int batchSize = 500, Func<T, bool> postFilter = null) where T : EntityObject
{
    var retList = new List<T>();

    // Build a new query with the current batch of IDs to retrieve and add it to the list we are returning
    newQuery = entityQuery.Where<T>(containsExpression(entityIds));
    retList.AddRange(newQuery.ToList());

    return retList;
} 

Call function:

var entities = BatchEntity.Load<ItemType>(from eItemType in dal.Context.InstanceContainer.ItemTypes
select eItemType
, itemTypeData
, (entityArray => (ek => entityArray.Contains(ek.ItemTypeId)))
);

It works fine. Is there any way I can make EF understand the more generic version?

  • 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-06-12T12:59:52+00:00Added an answer on June 12, 2026 at 12:59 pm

    The problem, as you describe, is that the entityKey function in the first example is opaque since it is of type Func rather than Expression. However, you can get the behavior you want by implementing a Compose() method to combine two expressions. I posted the code to implement compose in this question: use Expression<Func<T,X>> in Linq contains extension.

    With Compose() implemented, your function can be implemented as below:

    public static List<T> Load<T>(this IQueryable<T> entityQuery, 
                                  int[] entityIds, 
                                  // note that this is an expression now
                                  Expression<Func<T, int>> entityKey, 
                                  int batchSize = 500, 
                                  Expression<Func<T, bool>> postFilter = null) 
        where T : EntityObject
    {
        Expression<Func<int, bool>> containsExpression = id => entityIds.Contains(id);
        Expression<Func<T, bool>> whereInEntityIdsExpression = containsExpression.Compose(entityKey);
    
        IQueryable<T> filteredById = entityQuery.Where(whereInEntityIdsExpression);
    
        // if your post filter is compilable to SQL, you might as well do the filtering
        // in the database
        if (postFilter != null) { filteredById = filteredById.Where(postFilter); }
    
        // finally, pull into memory
        return filteredById.ToList();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a really simple prototype of a CMS system using ASP.Net
I'm trying to write a Linux device driver. I've got it to work really
While trying to write an answer for another SO question something really peculiar happened.
I'm trying to write a generic repository for my Entity Framework based application. Here's
I'm trying to write a class that has a generic member variable but is
I'm trying to write some reasonably generic networking code. I have several kinds of
I am trying to write my first ASP.NET website and I really want to
I'm trying to write a java application in Eclipse. I'm really wanting to use
I'm trying to write a query in NHibernate. I don't really care if I
Im trying to write an SQL query that will check in the table ‘persons’

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.