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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:27:58+00:00 2026-06-07T08:27:58+00:00

I have a DataSet with several rows and columns (as DataSets tend to have).

  • 0

I have a DataSet with several rows and columns (as DataSets tend to have). I need to create a tally row on the bottom with sums of each column. I’d like to do this with a single LINQ expression, as it will simplify a bunch of my code. I can get the total of a single column like so:

var a = (from m in month
         where <some long expression>
         select m["BGCO_MINUTES"] as Decimal?).Sum();

However, I want totals for other columns as well. I don’t want to use multiple LINQ expressions because there’s also a complicated where clause in there, and I’m doing several tally rows with various expressions and only want to loop through this set once. I also don’t want to manually loop through the dataset myself and add up the totals since I’m creating many of these tally rows and think it would be messier.

What I want is an anonymous type that contains a total of BGCO_MINUTES, 800IB_MINUTES and TSDATA_MINUTES.

Is there any way to do this?

  • 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-07T08:28:00+00:00Added an answer on June 7, 2026 at 8:28 am

    You could do this:

    // run the filters once and get List<DataRow> with the matching rows
    var list = (from m in month
                where <some long expression>
                select m).ToList();
    
    // build the summary object
    var result = new {
        BGCO_MINUTES = list.Sum(m => m["BGCO_MINUTES"] as Decimal?),
        _800IB_MINUTES= list.Sum(m => m["800IB_MINUTES"] as Decimal?),
    }
    

    And that’s assuming your where clause is not just long to type, but computationally expensive to evaluate. That will iterate through the list once per column.

    If you really want to only iterate the list once, you can probably do it with Enumerable.Aggregate, but the code is less elegant (in my opinion):

    // run the filters once and get List<DataRow> with the matching rows
    var a = (from m in month
             where <some long expression>
             select m)
            .Aggregate(           new { BGCO_MINUTES  = (decimal?)0m,
                                       _800IB_MINUTES = (decimal?)0m },
                        (ac,v) => new { BGCO_MINUTES  = ac.BGCO_MINUTES + (decimal?)v["BGCO_MINUTES"],
                                      _800IB_MINUTES = ac._800IB_MINUTES + (decimal?)v["800IB_MINUTES"] });
    

    Like I said, I think it’s less elegant than the first version, but it should work. Even though the first one requires a temporary copy of the values that match the where clause (memory cost) and 1 pass through the list for each field (CPU cost), I think it’s a lot more readable than the latter version – make sure the performance difference is worth it before using the less-understandable version.

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

Sidebar

Related Questions

I have a dataset called results with several rows of data. I'd like to
I have DataSet which has 3 columns. Name - insurance comp. name - treatmentDate
I have a dataset with ~20M rows and I'm observing the following behavior. The
I have a DataSet , where in the first table and in the row
I have used gmt to create several .ps files of x - y graphs
What is the best way for me to have several datasets ? i've thought
I have a strongly typed ADO.NET dataset containing several tables exported as an XML
I get several Datasets listed in a single xml File. I have to extract
I have a several rows in my table and I want to export the
I am newbie to redis, and I have a dataset of several million member

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.