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

  • Home
  • SEARCH
  • 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 9223323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:03:09+00:00 2026-06-18T04:03:09+00:00

Datatable as below: Item CartonID Quantity A 0001 1000 A 0002 500 A 0003

  • 0

Datatable as below:

Item CartonID Quantity
A    0001     1000
A    0002     500
A    0003     250
A    0002     500
B    0002     500
B    0003     250

My output suppose to be this:

ItemNo CartonID        TotalCarton TotalQuantity
A      0001,0002,0003  3           2250
B      0002,0003       2           750

But my result is list as below:

ItemNo  CartonID            TotalCarton  TotalQuantity
    A   0001, 0002, 0003    3              2,250
    B   0002, 0003          2                750

My code is list as below:

var items = ( from item in dtTest.AsEnumerable()
              group item by new
              {
                  Item_No=item.Field<string>("Item")
              }
              into g
              select new
              {
                  g.Key.Item_No,
                  TotalCarton = (from p in dtTest.AsEnumerable()
                                 where p.Field<string>("Item") == g.Key.Item_No
                                 select p.Field<string>("CartonID")).Distinct().ToArray().Count(),
                  Total_Quantity =g.Sum((p=>p.Field<decimal>("Quantity"))).ToString("###,###,###,###"),
                  CartonID = (from p in dtTest.AsEnumerable()
                              where p.Field<string>("Item") == g.Key.Item_No
                              select p.Field<string>("CartonID")).Distinct().ToArray().Aggregate
                              ((p1, p2) => p1 + ", " + p2)
                 }).ToList();

Anyone can tell me how to sum the total with distinct Carton ID. Thanks in advance.

  • 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-18T04:03:10+00:00Added an answer on June 18, 2026 at 4:03 am

    You should GroupBy CartonID and then sum the groups elements:

    something like this

    dtTest.GroupBy(x => x.CartonId)
          .ToDictionary(y => y.Key, y => y.Sum(z => z.Quantity);
    

    This will return a dictionary containing the total quantity per each CartonID. I hope i understood correctly that this is what you want.


    Based on the Output that you said is supposed to be retrieved, i adapted the query to this:

    dtTest.GroupBy(x => x.ItemNo)
          .Select(itemGroup => new { 
                  itemGroup.Key,
                  itemGroup.Distinct(item => item.CartonID), 
                  itemGroup.Distinct(item => item.CartonID).Count(),
                  itemGroup.Sum(item => item.Quantity)
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 collection datatable that shows in this below Month: January ID NAME
I have a h:dataTable that displays ProfileNotification like below: <h:dataTable value=#{myBean.profileNotifications} var=item rendered=#{myBean.renderProfileNotification}> <h:column>
I've a data table as below: <h:dataTable value=#{bean.items} var=item> I'd like to populate it
The whole syntax is as below <h:commandLink action=CustomerDetails?faces-redirect=true&amp;customerId=#{item.id} value=#{item.name}/> This is inside a dataTable,
I've the below form: <h:form> <h:dataTable value=#{bean.items} var=item> <h:column> <h:selectBooleanCheckbox value=#{item.enabled} valueChangeListener=#{bean.onchangeEnabled}> <f:ajax event=change
I have the datatable below : qty price date 5 2 1/25 6 1
I have a DataTable MyDT like below: string MyConString = SERVER= + sConfig_hostname +
I am using the below code to Export DataTable to MS Word,Excel,CSV format &
I'm using below code to check some form fields and render datatable table on
I am exporting an .xls file into datatable. Below are my code: private DataTable

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.