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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:41:20+00:00 2026-06-02T00:41:20+00:00

The actual query I am trying to write is slightly trickier than the title

  • 0

The actual query I am trying to write is slightly trickier than the title suggests. I have a list of orders like such: List<Order>, an order looks like this:

public class Order
{
    private StockCodes _stockCode;
    private bool _bidSide;
    private int _volume;
    private decimal _price;
}

I need to publish the best bid price and volume and the best sell price and volume given a specific stock code. The best bid price is defined as the HIGHEST price where bidSide is true. The best sell price is defined as the LOWEST price where bidSide is false.

For example given the following data for stock code “ABC”:

 { bidSide: true, volume: 25, price: 25  }
 { bidSide: true, volume: 25, price: 25  }
 { bidSide: true, volume: 25, price: 5  }

 { bidSide: false, volume: 100, price: 1  }
 { bidSide: false, volume: 50, price: 2}
 { bidSide: false, volume: 75, price: 8 }

Best bid: price 25, volume 50 (since there are 2 orders at the highest price)
Best sell: price 1, volume 100 (since there is just 1 order at the lowest price)

Lastly, I need to account for when there are no bid or sell orders. Efficiency is of high priority, so If I am able to do this in one linq statement that would be preferred.

  • 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-02T00:41:21+00:00Added an answer on June 2, 2026 at 12:41 am

    To do this efficiently, you really only want to iterate over the data once. Unfortunately, that makes it a real pain to implement with LINQ, as there’s quite a lot of work to do.

    Personally I would suggest that you don’t do this with LINQ – you could implement it with Aggregate, but it wouldn’t be terribly pleasant. It’s not too bad with a simple foreach loop though. Something like:

    int buyVolume = -1;
    int sellVolume = -1;
    decimal buyPrice = decimal.MinValue;
    decimal sellPrice = decimal.MaxValue;
    
    foreach (var order in orders)
    {
        if (order.bidSide)
        {
            if (order.Price > buyPrice)
            {
                buyPrice = order.Price;
                buyVolume = order.Volume;
            }
            else if (order.Price == buyPrice)
            {
                buyVolume += order.Volume;
            }
        }
        else
        {
            if (order.Price < sellPrice)
            {
                sellPrice = order.Price;
                sellVolume = order.Volume;
            }
            else if (order.Price == sellPrice)
            {
                sellVolume += order.Volume;
            }
        }
    }
    
    // Check sellVolume == -1 to verify whether we've seen any sale orders
    // Check buyVolume == -1 to verify whether we've seen any buy orders
    // Use buyPrice/buyVolume and sellPrice/sellVolume otherwise
    

    Doing it as efficiently as possible in LINQ would effectively mean putting all that logic in the loop into a function to pass into Aggregate – and you’d probably want to create a custom value type to hold the four values, to avoid creating more objects than you need to. That may be overkill, but you did say you wanted it as efficient as possible…

    • 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 query that returns the user ID's of the top
I am trying to write a query to insert a value into a timestamp
I am trying to use Zend_Db_Select to write a select query that looks somewhat
I'm trying to figure out how to work out this query. I have a
I'm trying to write a query that reports the current database activity. The query
I'm trying to analyse a query execution plan in my Oracle database. I have
I have this query, that's been giving me some issues, it looks like this:
I have been trying to query the increase percentage of a product grouped in
I'm having difficulty trying to form the actual question I have, so hopefully I
I have a query like : SELECT .. FROM ... WHERE ... AND ISNULL(ICA.actual_appearance_date,

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.