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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:20:14+00:00 2026-05-25T13:20:14+00:00

Take the following LINQ query as an example. Please don’t comment on the code

  • 0

Take the following LINQ query as an example. Please don’t comment on the code itself as I’ve just typed it to help with this question.

The following LINQ query uses a ‘group by’ and calculates summary information. As you can see there are numerous calculations which are being performed on the data but how efficient is LINQ behind the scenes.

var NinjasGrouped = (from ninja in Ninjas 
    group pos by new { pos.NinjaClan, pos.NinjaRank } 
    into con 
    select new NinjaGroupSummary 
    { 
        NinjaClan = con.Key.NinjaClan, 
        NinjaRank = con.Key.NinjaRank, 
        NumberOfShoes = con.Sum(x => x.Shoes), 
        MaxNinjaAge = con.Max(x => x.NinjaAge), 
        MinNinjaAge = con.Min(x => x.NinjaAge), 
        ComplicatedCalculation = con.Sum(x => x.NinjaGrade) != 0 
        ? con.Sum(x => x.NinjaRedBloodCellCount)/con.Sum(x => x.NinjaDoctorVisits)
        : 0,
    ListOfNinjas = con.ToList() 
    }).ToList(); 
  1. How many times is the list of ‘Ninjas’ being iterated over in order to calculate each of the values?
  2. Would it be faster to employ a foreach loop to speed up the execution of such a query?
  3. Would adding ‘.AsParallel()’ after Ninjas result in any performance improvements?
  4. Is there a better way of calculating summery information for List?

Any advice is appreciated as we use this type of code throughout our software and I would really like to gain a better understanding of what LINQ is doing underneath the hood (so to speak). Perhaps there is a better way?

  • 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-25T13:20:15+00:00Added an answer on May 25, 2026 at 1:20 pm

    Assuming this is a LINQ to Objects query:

    • Ninjas is only iterated over once; the groups are built up into internal concrete lists, which you’re then iterating over multiple times (once per aggregation).
    • Using a foreach loop almost certainly wouldn’t speed things up – you might benefit from cache coherency a bit more (as each time you iterate over a group it’ll probably have to fetch data from a higher level cache or main memory) but I very much doubt that it would be significant. The increase in pain in implementing it probably would be significant though 🙂
    • Using AsParallel might speed things up – it looks pretty easily parallelizable. Worth a try…
    • There’s not a much better way for LINQ to Objects, to be honest. It would be nice to be able to perform the aggregation as you’re grouping, and Reactive Extensions would allow you to do something like that, but for the moment this is probably the simplest approach.

    You might want to have a look at the GroupBy post in my Edulinq blog series for more details on a possible implementation.

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

Sidebar

Related Questions

Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
Take the following code example: File package1/__init__.py : from moduleB import foo print moduleB.__name__
Take the following two lines of code: for (int i = 0; i <
Take the following string as an example: The quick brown fox Right now the
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Take the following class as an example: class Sometype { int someValue; public Sometype(int
My current LINQ query and example XML are below. What I'd like to do
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I have the following LINQ conditional where clause query that produces a result of
This is probably quite trivial but.... I have a quite complicated linq query that

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.