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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:53:51+00:00 2026-05-25T14:53:51+00:00

Given class: public class FooAmounts int Id decimal Month1 decimal Month2 … decimal Month12

  • 0

Given class:

public class FooAmounts
int Id
decimal Month1
decimal Month2
...
decimal Month12
decimal Year2
decimal Year3
decimal Future

I have IEnumerable<FooAmounts> with 5 entries (Id’s 1-5 funnily enough!)

I’d like to create a single new FooAmounts which has the totals of each month/year but only where Id==1 + Id==2
e.g.

var footle = (from f in foolist
where f.Id == 1 || f.Id == 2
select new FooAmounts{
Month1 = Sum(month 1s),
Month2 = Sum(month 2s),
etc etc.
Future = Sum(futures)
).FirstOrDefault();

I am trying to re-create this t-sql:

select SUM(Month1) as Month1, SUM(Month2) as Month2, SUM(Month3) as Month3 from FooAmount where Id=1 or Id=2"

I can acheive similar using a group by f.Id into grp clause but that leaves me with two items and therefore I can’t use First/FoD, meaning I would have to manually Sum them still…which seems like I must be missing a trick somewhere?

Note: The FirstOrDefault() is only there so I get back a single object rather than an enumerable containing one object. I think (perhaps wrongly!) that this should always return only one item anyway…there is only one result of a Sum(), regardless as to how many items went into the calculation.

  • 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-25T14:53:52+00:00Added an answer on May 25, 2026 at 2:53 pm

    Regardless of whether you have one value or many, it doesn’t make sense to take the first result of a sum.

    If I have 10 people, I can find the age of the first one, or I can find the sum of the ages of all of them – but I can’t find the first sum of the ages. So you can do:

     var matches = fooList.Where(f => f.Id == 1 || f.Id == 2);
    
     var sum = new FooAmounts { Month1 = matches.Sum(f => f.Month1),
                                Month2 = matches.Sum(f => f>Month2),
                                ... };
    

    Now that will execute the query multiple times, of course. You can materialize the query instead, and then do the summing over that:

     // Materialize the result so we only filter once
     var matches = fooList.Where(f => f.Id == 1 || f.Id == 2).ToList();
    
     var sum = new FooAmounts { Month1 = matches.Sum(f => f.Month1),
                                Month2 = matches.Sum(f => f>Month2),
                                ... };
    

    Or alternatively you could use aggregation:

     var sum = fooList.Where(f => f.Id == 1 || f.Id == 2)
                      .Aggregate(new FooAmounts(), // Seed
                                 (sum, item) => new FooAmounts {
                                     Month1 = sum.Month1 + item.Month1,
                                     Month2 = sum.Month2 + item.Month2,
                                     ...
                                 });
    

    This will only iterate over the sequence once, and not create a big buffer in memory, but will create a lot of FooAmounts instances as it iterates.

    You could modify the accumulator in place, of course:

     var sum = fooList.Where(f => f.Id == 1 || f.Id == 2)
                      .Aggregate(new FooAmounts(), // Seed
                                 (sum, item) => {
                                     sum.Month1 += item.Month1;
                                     sum.Month2 += item.Month2;
                                     ...
                                     return sum;
                                 });
    

    That’s feels slightly nasty to me, but it doesn’t really have side-effects in a bad way, as it’s only mutating the object which is initialized within the call anyway.

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

Sidebar

Related Questions

I have class Foo with a constructor as given: class Foo { public: Foo(int
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
Given the following class public class Foo { public int FooId { get; set;
I have a data structure which is as given below: class File { public
Given the following class: class TestClass { public void SetValue(int value) { Value =
Given a class: class foo { public string a = ; public int b
given public Class Example { public static void Foo< T>(int ID){} public static void
Given public class Blah : IBlah { public Blah(decimal argument) { } } When
Given a class like below: public class LoginInfo { public int UserId; public string
Given: class Hokey { public: explicit C(int i): i_(i) { } template<typename T> T&

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.