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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:16:50+00:00 2026-05-15T15:16:50+00:00

In a list of transaction objects, I am attempting to group by BatchNo then

  • 0

In a list of transaction objects, I am attempting to group by BatchNo then sum the Amounts.

public class Extract
{
    // Notable fields in TRANSACTION are: String mBatchNo, String mAmount
    private List<Transaction> Transactions;

    public void testTransactions()
    {

        // Sum of amounts grouped by batch number
        var sGroup = from t in Transactions
                     group t by t.mBatchNo into g
                     select new { batchNo = g.Key, 
                                  totalAmount = g.Max(a => (Int32.Parse(a.mAmount)))};
    }
}

At this point, I step into the code look through the locals window to see what my result set is to check against the file I’ve imported to this object.

The last batch in the file has 3 records, 100 amount each which can be seen drilling into the Transactions list object. However drilling down into the sGroup result finds the same batch to have 100 amount total (Should be 300). What have I messed up in this query?

Note that I’ve stored this as a string since we’re zero filled to the left of a 8 character field. For export reasons I decided to store as a string. Though this can (and probably will) be changed it does not answer my question: How to make this query aggregate the sum into sets by BatchNo?

  • 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-15T15:16:51+00:00Added an answer on May 15, 2026 at 3:16 pm

    You need to call Sum instead of Max:

    var sGroup = from t in Transactions
        group t by t.mBatchNo into g
        select new {
            batchNo = g.Key, 
            totalAmount = g.Sum(a => (int.Parse(a.mAmount))) // Sum, not Max
        };
    

    I would also suggest, if your mAmount field is stored as a string, to use a more robust method than int.Parse (since that will throw an exception if the string is not a valid integer, e.g., if it’s blank). Something like this:

    int ParseOrZero(string text)
    {
        int value;
        if (int.TryParse(text, out value))
            return value;
        else
            return 0;
    }
    
    var sGroup = from t in Transactions
        group t by t.mBatchNo into g
        select new {
            batchNo = g.Key, 
            totalAmount = g.Sum(ParseOrZero) // blanks will be treated as 0
        };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static List<PreviewSchedule> BuildPreviewSchedule(Chatham.Business.Objects.Transaction transaction) { List<PreviewSchedule> items = new List<PreviewSchedule>(); List<ScheduleItem> scheduleItems =
I have following entity (non-relevant fields/methods are removed). public class HitsStatsTotalDO { @Id transient
Using hibernate and Hsqldb - a list of objects are session.merged through a transaction.
I have list of transaction objects and want to order them by certain condition
List<CertMail> lsRecipetNumber = new List<CertMail>(); CertMail class is in data access layer which returns
I am retrieving a list of objects in hibernate using Criteria API. However I
According Martin Fowler's Unit of Work description: Maintains a list of objects that are
I am using hibernate to insert objects of the class meal in a hsql
I have a list of 10 data objects that I want to insert/update to
I'm trying to rewrite the following java method that return a list of objects(hibenrate

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.