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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:52:16+00:00 2026-06-03T01:52:16+00:00

So I have a very simple structure: I have Orders that have a unique

  • 0

So I have a very simple structure:

  • I have Orders that have a unique OrderNumber
  • Orders have many OrderRows
  • OrderRows have many RowExtras that have 2 fields, position (the sequence number of the RowExtra within the OrderRow) and Info, which is a string. More often than not, an OrderRow does not have more than one RowExtra.

(Don’t mind the silly structure for now, it’s just how it is).

So now I get a list of objects that have three properties:

  • OrderNumber
  • Position
  • Info

What I want to do is simply 1) check if the RowExtra with the given OrderNumber/Position -pair exists in the database and if so, 2) update the Info-property.

I have tried a few different ways to accomplish this with very poor results at best. The solutions loop through the list of objects and issue a query such as

myContext.RowExtras.Where(x => x.Position == currentPosition &&
                          x.OrderRow.Order.OrderNumber == currentOrderNumber) 

or going from the other side

myContext.Orders.Where(x => x.OrderNumber == currentOrderNumber)
                .SelectMany(x => x.OrderRows)
                .SelectMany(x => x.RowExtras)
                .Where(x => x.Position == currentPosition)

and then check if the count equals to 1 and if so, update the property, otherwise proceed to next item.

I currently have roughly 4000 RowExtras in the database and need to update about half of them. These methods make the procedure take several minutes to complete, which is really not acceptable. What I don’t understand is why it takes such a long time, because the SQL-clause that returns the required RowExtra would be quite easy to write manually (with just 2 joins and 2 conditions in the where-part).

The best performance I managed to achieve was with a compiledquery looking like this

Func<MyContext, int, string, IQueryable<RowExtra>> query = 
CompiledQuery.Compile( 
  (MyContext ctx, int position, string orderNumber) => 
    from extra in ctx.RowExtras 
    where 
      extra.Position == position && 
      extra.OrderRow.Order == orderNumber 
    select extra);

and then invoking said query for each object in my list. But even this approach took way over a minute. So how do I actually get this thing to run within a reasonable timeframe?

Also, I’m sorry for the overly long explanation, but hopefully someone can help me!

  • 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-03T01:52:17+00:00Added an answer on June 3, 2026 at 1:52 am

    Try to minimise the number of database calls. As a rule of thumb, each one will take roughly 10ms at least – even one that just returns a scalar.

    So, in general, fetch all the data you will need in one go, modify it in code and then save it.

    List<Order> orders = myContext.Orders
        .Include( "OrderRows.RowExtras" )
        .Where( ... select all the orders you want, not just one at a time ... )
        .ToList();
    
    foreach ( Order order in orders )
    {
        ... execute your logic on the in-memory model
    }
    
    myContext.SaveChanges();
    
    • 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 binary tree structure, something like: struct nmbintree_s { unsigned
I have a very simple webforms app that will allow field techs to order
I have a very simple Setup project that copies three dlls into the GAC.
I have a very simple iPhone app that requires a random integer from 1-100.
I have a very simple Mac Cocoa app that just has a Web View
I have a very simple python script that should scan a text file, which
I have a very simple structure as shown below. what I am trying to
I have a very simple table structure here. Just a list of words related
I have a very simple class that I'm trying to serialize: [ProtoContract] public class
Hopefully this is very simple. I have a central activity that can be launched

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.