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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:35:52+00:00 2026-05-25T22:35:52+00:00

I have a situation where my application constructs a dynamic LINQ query using PredicateBuilder

  • 0

I have a situation where my application constructs a dynamic LINQ query using PredicateBuilder based on user-specified filter criteria (aside: check out this link for the best EF PredicateBuilder implementation). The problem is that this query usually takes a long time to run and I need the results of this query to perform other queries (i.e., joining the results with other tables). If I were writing T-SQL, I’d put the results of the first query into a temporary table or a table variable and then write my other queries around that. I thought of getting a list of IDs (e.g., List<Int32> query1IDs) from the first query and then doing something like this:

var query2 = DbContext.TableName.Where(x => query1IDs.Contains(x.ID))

This will work in theory; however, the number of IDs in query1IDs can be in the hundreds or thousands (and the LINQ expression x => query1IDs.Contains(x.ID) gets translated into a T-SQL “IN” statement, which is bad for obvious reasons) and the number of rows in TableName is in the millions. Does anyone have any suggestions as to the best way to deal with this kind of situation?

Edit 1: Additional clarification as to what I’m doing.

Okay, I’m constructing my first query (query1) which just contains the IDs that I’m interested in. Basically, I’m going to use query1 to “filter” other tables. Note: I am not using a ToList() at the end of the LINQ statement—the query is not executed at this time and no results are sent to the client:

var query1 = DbContext.TableName1.Where(ComplexFilterLogic).Select(x => x.ID)

Then I take query1 and use it to filter another table (TableName2). I now put ToList() at the end of this statement because I want to execute it and bring the results to the client:

var query2 = (from a in DbContext.TableName2 join b in query1 on a.ID equals b.ID select new { b.Column1, b.column2, b.column3,...,b.columnM }).ToList();

Then I take query1 and re-use it to filter yet another table (TableName3), execute it and bring the results to the client:

var query3 = (from a in DbContext.TableName3 join b in query1 on a.ID equals b.ID select new { b.Column1, b.column2, b.column3,...,b.columnM }).ToList();

I can keep doing this for as many queries as I like:

var queryN = (from a in DbContext.TableNameN join b in query1 on a.ID equals b.ID select new { b.Column1, b.column2, b.column3,...,b.columnM }).ToList();

The Problem: query1 is takes a long time to execute. When I execute query2, query3…queryN, query1 is being executed (N-1) times…this is not a very efficient way of doing things (especially since query1 isn’t changing). As I said before, if I were writing T-SQL, I would put the result of query1 into a temporary table and then use that table in the subsequent queries.

Edit 2:

I’m going to give the credit for answering this question to Albin Sunnanbo for his comment:

When I had similar problems with a heavy query that I wanted to reuse in several other queries I always went back to the solution of creating a join in each query and put more effort in optimizing the query execution (mostly by tweaking my indexes).

I think that’s really the best that one can do with Entity Framework. In the end, if the performance gets really bad, I’ll probably go with John Wooley’s suggestion:

This may be a situation where dropping to native ADO against a stored proc returning multiple results and using an internal temp table might be your best option for this operation. Use EF for the other 90% of your app.

Thanks to everyone who commented on this post…I appreciate everyone’s input!

  • 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-25T22:35:53+00:00Added an answer on May 25, 2026 at 10:35 pm

    If the size of TableName is not too big to load the whole table you use

    var tableNameById = DbContext.TableName.ToDictionary(x => x.ID);
    

    to fetch the whole table and automatically put it in a local Dictionary with ID as key.

    Another way is to just “force” the LINQ evaluation with .ToList(), in the case fetch the whole table and do the Where part locally with Linq2Objects.

    var query1Lookup = new Hashset<int>(query1IDs);
    var query2 = DbContext.TableName.ToList().Where(x => query1IDs.Contains(x.ID));
    

    Edit:
    Storing a list of ID:s from one query in a list and use that list as filter in another query can usually be rewritten as a join.
    When I had similar problems with a heavy query that I wanted to reuse in several other queries I always went back to the solution of creating a join in each query and put more effort in optimizing the query execution (mostly by tweaking my indexes).

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

Sidebar

Related Questions

I have a situation where in a web application a user may need a
I have a situation where I user IoC (WindsorContainer) in a .Net web application,
I'm using Java sockets for client - server application. I have a situation when
I've got a situation where I need to have my LINQ to Entities query
I have the following situation: my application's authorization mechanism is implemented using Spring security.
We have a situation where our application calls some stored procedures on a sql
In my application I have a situation where we need to capture the when
I have a situation where i need to debug a Windows CE application in
My Situation: I have 1 asp.net application with both aspx pages AND webservices I
I have the following situation: multiple virtual directories under same application pool in IIS

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.