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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:31:08+00:00 2026-06-01T05:31:08+00:00

I have 2 datatables in a dataset. One table has a list called CostTypes.

  • 0

I have 2 datatables in a dataset. One table has a list called CostTypes. Just an Id and Description field.

The other datatable is the master table and has many records and one of the columns is the cost type. There will be cost types that are not reference in this datatable. There is another column in this databale called cost.

What I am trying to do is get a summary by cost type with a total of the cost. But I want ALL cost types listed any values not in the master table will be zero.

CostType table

Id, Description

1,Marketing

2,Sales

3,Production

4,Service

Master table

Id, Cost, CostTypeId

1,10,1

2,120,1

3,40,3

So I would like to see a result in a datable (if possible) so I can bind to datagridview

Marketing  130

Sales       0

Production  40

Service     0

Thanks for the help everyone, this is what I came up from the answers – Can anyone suggest any improvements???

Also how can I convert the result in query1 into a datable???

  var query1 =
        from rowCT in costTypes.AsEnumerable()
        from rowSTD in stdRates.AsEnumerable()
              .Where( d => d.Field<int?>( "CostTypeId" ) == rowCT.Field<int?>( "CostTypeId" ) )
              .DefaultIfEmpty()
        group new { row0 = rowCT, row1 = rowSTD }
        by rowCT.Field<string>( "Description" ) into g
        select new
        {
            g.Key,
            Cost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "Cost" ) ),
            TotalCost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalCost" ) ),
            TotalHours = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalHours" ) ),
            TotalLabourCost = g.Sum( x => x.row1 == null ? 0 : x.row1.Field<decimal>( "TotalLabourCost" ) )
        }
        ;
  • 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-01T05:31:09+00:00Added an answer on June 1, 2026 at 5:31 am

    Maybe something like this:

    Test data:

    DataTable dt=new DataTable();
    dt.Columns.Add("Id",typeof(int));
    dt.Columns.Add("Description",typeof(string));
    
    dt.Rows.Add(1,"Marketing");
    dt.Rows.Add(2,"Sales");
    dt.Rows.Add(3,"Production");
    dt.Rows.Add(4,"Service");
    
    DataTable dt2=new DataTable();
    dt2.Columns.Add("Id",typeof(int));
    dt2.Columns.Add("Cost",typeof(int));
    dt2.Columns.Add("CostTypeId",typeof(int));
    
    dt2.Rows.Add(1,10,1);
    dt2.Rows.Add(2,120,1);
    dt2.Rows.Add(3,40,1);
    

    Linq query

    var query=(
                from row in dt.AsEnumerable()
                from row1 in dt2.AsEnumerable()
                      .Where (d =>d.Field<int>("Id")==row.Field<int>("Id") )
                      .DefaultIfEmpty()
                group new{row,row1} 
                by row.Field<string>("Description") into g 
                select new
                {
                    g.Key,
                    Cost=g.Sum (x =>x.row1==null?0:x.row1.Field<int>("Cost"))
                }
                );
    

    Result

    Key        Cost
    Marketing  10
    Sales      120
    Production 40
    Service    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Shoppings in my SQL db. This one has a
Given a DataSet, I'm left joining DataTables[1-n] onto DataTable[0]. I have created a method
I have a dataset with just 1 datatable and 1 row but with 2
I have two DataTables one is Primary Table and the rest is a sub
i have windows application in which i am constructing dataset with two datatables one
I have a strongly typed dataset containing a datatable with one of the columns
I have a table that has a field, IsActive, that indicates whether a record
I have a DataSet with some DataTables that are linked together with DataRelations (classic
I have a DataSet with some Datatables and I am saving this DataSet as
I have seen blogs and people saying Returning dataset/datatable from an ajax enabled wcf

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.