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

  • Home
  • SEARCH
  • 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 8450129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:53:20+00:00 2026-06-10T10:53:20+00:00

I have written the following linq statement but I can’t help feeling it could

  • 0

I have written the following linq statement but I can’t help feeling it could be simplified somehow. The purpose is to take a List<IEnumberable<Counterparty>> (counterpartyStatic in the query) objects and find the Orders that have a ClientId equal to Counterparty.CounterpartyId. Those order should then have the field ClientDesc updated to match the Counterparty.DescriptionField. Once this is done I need to raise an event passing all the orders that have been updated.

The OrderCache is a dictionary in case that is not obvious.

Here is the existing code:

var updates = new List<Order>();
        lock (CacheLock)
        {
            counterpartyStatic.ToList().ForEach(cachedList =>
                cachedList.ToList().ForEach(
                    counterparty =>
                    {
                        var orders = OrderCache.Where(kvp => kvp.Value.Client == counterparty.CounterpartyId);
                        orders.ToList().ForEach(kvp =>
                        {
                            kvp.Value.ClientDesc = counterparty.Description;
                            updates.Add(kvp.Value);
                        });
                    }));
        }

        RaiseEvent(updates);

Thanks for any help

  • 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-10T10:53:21+00:00Added an answer on June 10, 2026 at 10:53 am

    Given that your “query” is really intended to cause side effects (ie: you’re changing the value’s ClientDesc in a deep nested loop), I would suggest writing this using loops instead of trying to use LINQ. This has a couple of advantages – you’re not making lists just to use List<T>.ForEach, and the intention is far more clear:

    var updates = new List<Order>();
    
    lock (CacheLock)
    {
        foreach(var cachedList in counterpartyStatic)
        {
            foreach(var counterparty in cachedList)
            {
                 var orders = OrderCache.Where(kvp => kvp.Value.Client == counterparty.CounterpartyId);
                 foreach(var kvp in orders)
                 {
                      kvp.Value.ClientDesc = counterparty.Description;
                      updates.Add(kvp.Value);
                 }
             }
        }
    
        RaiseEvent(updates);
    }
    

    This is just as short as your original, but far more clear in terms of what is happening.

    For an argument on why avoiding List<T>.ForEach can be beneficial, see Eric Lippert’s “foreach” vs “ForEach”. List<T>.ForEach has even been removed for the new Windows Store apps, given the issues surrounding it.

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

Sidebar

Related Questions

I have written following regex But its not working. Can you please help me?
I have written the following query, but in the WHERE clause I have used
I have written a LINQ to XML query that does what I want, but
I have written the following method to return a list of Unserializable classes (LINQ
I am very new in LINQ I have written following query : var duplicate
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written the following LINQ-to-SQL to enable me to get a total number
I have written following lines to click all links in fetched page but it
I have written following code to pass File1 variable to Javascript, but its not
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];

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.