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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:41:59+00:00 2026-05-13T01:41:59+00:00

From the Data OrderID OrderAmt OrderDate ———– ———- ——————– 1 10.50 2003-10-11 08:00:00 2

  • 0

From the Data

OrderID     OrderAmt   OrderDate                                              
----------- ---------- --------------------
1           10.50      2003-10-11 08:00:00
2           11.50      2003-10-11 10:00:00
3           1.25       2003-10-11 12:00:00
4           100.57     2003-10-12 09:00:00
5           19.99      2003-10-12 11:00:00
6           47.14      2003-10-13 10:00:00
7           10.08      2003-10-13 12:00:00
8           7.50       2003-10-13 19:00:00
9           9.50       2003-10-13 21:00:00

I want to display the following running total

OrderId     OrderDate            OrderAmt   Running Total                        
----------- -------------------- ---------- ------------- 
1           2003-10-11 08:00:00  10.50      10.50
2           2003-10-11 10:00:00  11.50      22.00
3           2003-10-11 12:00:00  1.25       23.25
4           2003-10-12 09:00:00  100.57     123.82
5           2003-10-12 11:00:00  19.99      143.81
6           2003-10-13 10:00:00  47.14      190.95
7           2003-10-13 12:00:00  10.08      201.03
8           2003-10-13 19:00:00  7.50       208.53
9           2003-10-13 21:00:00  9.50       218.03

From the following list

List<OrderData> ord = new List<OrderData>();

ord.Add(new OrderData() { OrderNumber=1, OrderAmount=10.50, 
OrderDate=new DateTime(2003,10,11)});

ord.Add(new OrderData() { OrderNumber=2, OrderAmount=11.50,
OrderDate=new DateTime(2003,10,11)});

ord.Add(new OrderData() { OrderNumber=3, OrderAmount=1.25,
OrderDate=new DateTime(2003,10,11)});

ord.Add(new OrderData() { OrderNumber=4, OrderAmount=100.57,
OrderDate =new DateTime(2003,10,12)});

ord.Add(new OrderData() { OrderNumber=5, OrderAmount=19.99,
OrderDate=new DateTime(2003,10,12)});

ord.Add(new OrderData() { OrderNumber=6, OrderAmount=47.14,
OrderDate =new DateTime(2003,10,13)});

ord.Add(new OrderData() { OrderNumber=7, OrderAmount=10.08,
OrderDate=new DateTime(2003,10,13)});

ord.Add(new OrderData() { OrderNumber=8, OrderAmount=7.50,
OrderDate=new DateTime(2003,10,13)});

ord.Add(new OrderData() { OrderNumber=9, OrderAmount=9.50,
OrderDate=new DateTime(2003,10,13)});

How to reshape the following query

var query = from o in ord
            select new
            {
             OrdNumber=o.OrderNumber,
             OrdDate=o.OrderDate,
             Amount=o.OrderAmount,
            RunntingTotal =ord.Aggregate(0,(o a,o b)=>
           {a.OrderAmount+b.OrderAmount;}) 
            };
  • 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-13T01:42:00+00:00Added an answer on May 13, 2026 at 1:42 am

    You can’t do this using the built-in Aggregate method.

    Darin’s answer will give you what you need, but has side-effects (ie, the query updates the total variable as it enumerates).

    If you want a “pure” query without side-effects then you’ll need to create some sort of Accumulate extension method and use that instead of Aggregate:

    var query = ord.Accumulate(
        new { Order = (OrderData)null, Total = 0.0 },
        (a, x) => new { Order = x, Total = a.Total + x.OrderAmount });
    
    foreach (var x in query)
    {
        Console.WriteLine("{0}\t{1}\t{2}\t{3}",
            x.Order.OrderNumber, x.Order.OrderDate, x.Order.OrderAmount, x.Total);
    }
    
    // ...
    
    public static class EnumerableExtensions
    {
        public static IEnumerable<TAccumulate> Accumulate<TSource, TAccumulate>(
            this IEnumerable<TSource> source,
            TAccumulate seed,
            Func<TAccumulate, TSource, TAccumulate> func)
        {
            if (source == null)
                throw new ArgumentNullException("source");
    
            if (func == null)
                throw new ArgumentNullException("func");
    
            TAccumulate accumulator = seed;
            foreach (TSource item in source)
            {
                accumulator = func(accumulator, item);
                yield return accumulator;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.