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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:17:11+00:00 2026-06-11T17:17:11+00:00

I have a database table that I am using as a source for a

  • 0

I have a database table that I am using as a source for a report. The table layout looks
like. Table 1 is subset of a larger database table. But for the report I only need certain columns So I am creating a new data table by using the function given below

Value  Description   Hours
    1          A           2
    2          B           3
    3          C           5
    1          A           3
    2          B           4
    2          B           3
    3          C           3


private DataTable CreateFocusOfEffortData()
    {
        var dtChartData = new DataTable();
        dtChartData.Columns.Add("Description", typeof(string));
        dtChartData.Columns.Add("Hours", typeof(Double));
        var myDataView = ReportData.DefaultView;
        myDataView.RowFilter = "ActivityUnitID = " + _ActivityUnitID;
        for (var i = 0; i < myDataView.ToTable().Rows.Count; i++)
        {
            var dataRow = new Object[dtChartData.Columns.Count];
            dataRow[0] = ReportData.Rows[i]["Description"];
            dataRow[1] = csaConvert.ToDouble(ReportData.Rows[i]["Hours"]);
            dataRow[2] = ReportData.Rows[i]["Value"];
            dtChartData.Rows.Add(dataRow);
        }
        return dtChartData;
    }

Now I need to design another report with summary data from above table. This table should
combine data for all the columns based on Column[“Value]. for example in my case we have three distinct values in Value column 1,2,3. The resulting table should have hours summed up for all the three values.

    Value   Description  Hours
    1         A           5
    2         B           10
    3         C           8

Now I can do this in sql by creating another stored proc but I would rather use my existing data-table and write a C# function to get the result. Is there a easier way to achieve this using LINQ?

  • 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-11T17:17:12+00:00Added an answer on June 11, 2026 at 5:17 pm

    You can use AsEnumerable() in order to use LINQ, then GroupBy by Value and Description and do Sum on Hours to set for the first row:

    var result = dtChartData.AsEnumerable()
                .GroupBy(row => new
                    {
                        Value = row.Field<int>("Value"),
                        Description = row.Field<string>("Description")
                    })
                .Select(g =>
                    {
                        var row = g.First();
                        row.SetField("Hours", g.Sum(r => r.Field<double>("Hours")));
    
                        return row;
                    });
    

    The result will return IEnumerable<DataRow> but if you want to get DataTable back, use:

    var resultTable = result.CopyToDataTable();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code to update a database table that looks like try {
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a database table with some rows that I want to fetch using
that my problem: I have database table like that: id (AI) market_id 1 6
I have a large database table that I need to display on a Windows
I have a database table with a field that I need to read from
I have a database table that has a Unique Key constraint defined to avoid
I have a database table that is a dictionary of defined terms -- key,
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I have a very simple database table that joins two other tables in a

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.