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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:32:00+00:00 2026-06-10T16:32:00+00:00

I have two data tables and would like to join the two tables to

  • 0

I have two data tables and would like to join the two tables to find the difference between the two columns, however it keeps returning null and erroring out at CopyToDataTable method as you cannot pass null into it. I have checked just before this code that there are exactly same data in TestOutput and ExpectedOutput tables.
Here’s my code:

IEnumerable<DataRow> diff =
  (from datarows1 in TestOutput.AsEnumerable()
   join datarows2 in ExpectedOutput.AsEnumerable() 
   on datarows1.Field<String>("external_id") equals datarows2.Field<String>("external_id")

   select new
   {
      KeyId = datarows1.Field<String>("external_id"),
      Difference = datarows1.Field<Decimal>("quantity") - datarows2.Field<Decimal>("quantity")
    }) as IEnumerable<DataRow>;

DataTable difference = diff.CopyToDataTable<DataRow>();

Error Message @ last row:

ArgumentNullException was unhandled – value cannot be null. Parameter name:source

  • 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-10T16:32:01+00:00Added an answer on June 10, 2026 at 4:32 pm

    The problem is the as IEnumerable<DataRow>cast. If you remove that line you will see that your query is selecting out an anonymous type with properties KeyID and Difference, not an IEnumerable. The as operator attempts to cast IEnumerable<DataRow> and returns a null if no such cast is possible. That is where your null reference exception is coming from.

    To fix this, you need to find a different technique of populating the DataTable from your IEnumerable<a'> (where a' is the anonymous type)

    DataTable difference = new DataTable();
    difference.Columns.Add("KeyID", typeof(string));
    difference.Columns.Add("Difference", typeof(decimal));
    
    foreach (var result in diff)
       difference.Rows.Add(result.KeyID, result.Difference);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to join data from tables from two databases. One is filed
I have two tables which I would like to place side-by-side exactly as they
I have two data.tables in R which have an identical set of columns. They
I have two tables, a Countries table and a Weather table. I would like
I have combined two tables using union all . However, the produced data has
i have two data tables with the same structure the first one has one
I have a database with two tables: data and file . file_id is a
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have two tables set up in phpmyadmin- table userid and table data. The
I have two different tables from which I need to pull data blogs which

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.