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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:34:23+00:00 2026-05-13T21:34:23+00:00

Take the following linq query: var summary = results.Select(r => new { TotalPopulation =

  • 0

Take the following linq query:

var summary = results.Select(r => 
                             new
                             {
                             TotalPopulation = results.Sum(s => s.Population),
                             TotalGross = results.Sum(s => s.Gross),
                             }).Distinct();

The above works fine, but is it the best way to do it. What does the r represent? Why do I need it?

Also, I would think that I could add the following to my query, but I can’t:

GrossPerPop = TotalPopulation / TotalGross

The above says TotalPopulation and Total Gross do not exist in the current context.

I also tried:

GrossPerPop = results.sum(s => s.Population) / results.Sum(s => s.Gross), 

The above says / can’t be applied to decimal? or double? Does this have anything to do with the fields being nullable? It just so happens I don’t have any Population fields or Gross fields with a null value, so I changed these to non-nullable fields in the sql table designer. However, if they do contain a 0 value, how could I check that within the Linq code?

  • 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-13T21:34:23+00:00Added an answer on May 13, 2026 at 9:34 pm

    A better way is something like:

    class Metrics {
        public int TotalPopulation { get; set; }
        public decimal TotalGross { get; set; }
        public decimal GrossPerPopulation {
            get {
                return TotalGross / TotalPopulation;
            }
        }
     }
    

    Then:

    var metrics = new Metrics {
                      TotalPopulation = results.Sum(s => s.Population), 
                      TotalGross = results.Sum(s => s.Gross)
                  };
    

    What you currently have is performing the above projection (albeit into an anonymous type) for every element of results and then throwing out all but one of the resulting projections. You could get by without creating an explicit type as I’ve done above and just use an anonymous type but that would be silly.

    What does the r represent?

    The r represents the name of the parameter in the anonymous method that you’ve defined.

    Why do I need it?

    When you write

    results.Select(expression)
    

    it is necessary that expression be a delegate that eats whatever the type of the elements of results are and returns some other type. That is IEnumerable<SomeType>.Select is for projecting a sequenece to another sequence. Therefore, you must pass a method that eats whatever the type of the elements of results are and returns some other type. One way of doing is that is by passing in an anonymous delegate. One way of defining an anonymous delegate is by using a lambda expression. When you say r => ... you are defining an anonymous method via a lambda expression.

    Also, I would think that I could add the following to my query, but I can’t:

    GrossPerPop = TotalPopulation / TotalGross

    That’s right, you can’t. Think of it like this. Say that you had an explicit type

    class Metrics {
        public int TotalPopulation { get; set; }
        public decimal TotalGross { get; set; }
        public decimal GrossPerPopulation { get; set; }
        public Metrics(
            int totalPopulation,
            decimal totalGross,
            decimal grossPerPopulation
        ) {
                TotalPopulation = totalPopulation;
                TotalGross = totalGross;
                GrossPerPopulation = grossPerPopulation;
        }
    }
    

    Should the following be legal?

    Metrics m = new Metrics(100, 100, m.TotalPopulation / m.TotalGross)
    

    Of course not! But that’s effectively what you are trying to do.

    GrossPerPop = results.Sum(s => s.Population) / results.Sum(s => s.Gross)

    The above says / can’t be applied to decimal? or double?

    I don’t see any reason for you to be getting such a message. In C#, the above is legal assuming that both s.Population and s.Gross are numeric types; they will just be implicitly promoted to the "right" type. This is true even if s.Population or s.Gross are numeric types.

    Do note however that gross per population should be

    results.Sum(s => s.Gross) / results.Sum(s => s.Population)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
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 C# class: c1 { event EventHandler someEvent; } If there are
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
Take the following function: DataTable go() { return someTableAdapter.getSomeData(); } When I set a
Take the following hypothetical situation, how would I implement it in MVC? All my
Take the following code for example; if (Convert.ToString(frm.WindowState) == Minimized) Layout.WindowState = Maximized; else
Take the following class as an example: class Sometype { int someValue; public Sometype(int
Take the following useless program: class Program { static void Main(string[] args) { IUnityContainer

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.