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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:58:49+00:00 2026-06-03T14:58:49+00:00

I have a scenario which is causing very long running processes and suspect it

  • 0

I have a scenario which is causing very long running processes and suspect it is our usage of LINQ to Entity.

BACKGROUND:
Project is using LINQ To Entity and Repository pattern to expose data to our logic layer. That is how it is and wont be changing.

ISSUE:
A certain scenario has cropped up which requires the selection of intersecting data from both inputs and other tables. Trying to optimise this, I first queried the DB for an Array of IDs which I intend to use for getting the intersecting data. I also have another Array of integers to use in my LINQ expression. I then build an expression to select data from the table in question with LINQ where and contains methods. This takes a long time to execute. Almost a minute.

To battle this, I have tried several LINQ techniques that all take almost the same time. For information sake, below is a sample of some of my approaches.

// FYI: tableTotalsIds contains 14,856 IDs as an example, built from a repository call
var tableTotalsIds = tableTotals.Select(s => s.Id).ToArray();
int[] ages = {25, 26, 27};

Expression<Func<TotalAgeCounts, bool>> ageFilter = 
    af => af.TableTotalsId != null &&
          tableTotalsIds.Contains(af.TableTotalsId.Value) &&
          ages.Contains(af.Age);

var directStartTime = DateTime.Now;
var directFetch = _ctx.TotalAgeCounts.Where(ageFilter).ToList();
var directBenchMark = DateTime.Now.Subtract(directStartTime).TotalSeconds;

var repositoryStartTime = DateTime.Now;
var repositoryFetch = _totalAgeCountsRepository
    .SelectAll(new Specification<TotalAgeCounts>(ageFilter));
var repositoryBenchMark = DateTime.Now.Subtract(repositoryStartTime).TotalSeconds;

In all cases, query time takes about 1 minute. The thing that jumps out at me is the huge number of tableTotalsIds being used in the .Contains() method, but I am not aware of other LINQ ways of achieving this.

Is there a more optimised way of doing this in LINQ?

At the moment I am thinking of dropping this query back into the DB as a simple join and skip the LINQ bottle neck here. But first I will try pulling the unfiltered data into memory and then use LINQ to join the data together and see how efficient that will be.

I am interested in how others have overcome similar bottle necks without rewriting the architecture of the application.

SOLUTION

As the commenters pointed out, LINQ optimisation was not happening due to my .ToArray(). The problem went deeper, as I was using our Repository implementation to build the tableTotalsIds which already cast the result to an IList, losing further LINQ/SQL Optimisation. Simply not using our Repository implementation to build the tableTotalsIds and query the dataContext directly, leaving the results as IQueryable fixed the issue.

  • 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-03T14:58:51+00:00Added an answer on June 3, 2026 at 2:58 pm

    The fact that you are calling ToArray is causing the filtering records to be pulled out of the db, only to be injected again as part of your query. This prevents the query optimizer from making good use of the fact that it already had what it needed right there. As you noted in commemts, removing the Tolist/toarray helped.

    As for the repository pattern, there’s no reason you can’t use it. You just don’t need a separate repository for every class; only for those which are important, root objects that you will query for.

    In this case,your helper table info can just be rolled up in the same query; the repository pattern does not demand you create a sepatate repository for it.

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

Sidebar

Related Questions

I have a very simple scenario which I cannot get to work. I am
I have a scenario which is causing strange behavior with WPF data binding and
I have a scenario in which there exists a LINQ resultset; I used the
I have a scenario in which I'm serving a file from codebehind. which file,
I have a scenario in which I have REST API which manages a Resource
I have implement a scenario which involves two way communication between child and parent
I have a performance run scenario which calls cd %AUTORUN_DIR% call abc.bat cd %AUTORUN_DIR%
I am new to RSpec and I have a test scenario in which I
I have a scenario where i have one control, which is using another control
Scenario I have a DevExpress DataGrid which is bound to a DataSet in C#.

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.