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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:16+00:00 2026-05-31T00:52:16+00:00

Could you please check the following what is wrong in this. I need union

  • 0

Could you please check the following what is wrong in this.

I need union of this but it returns 6 record instead of 5(because “Amir” occurs two times)

DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("Name"));
dt1.Rows.Add(dt1.NewRow()["Name"] = "Imran");
dt1.Rows.Add(dt1.NewRow()["Name"] = "Amir");
dt1.Rows.Add(dt1.NewRow()["Name"] = "Asif");

DataTable dt2 = new DataTable();
dt2.Columns.Add(new DataColumn("Name"));
dt2.Rows.Add(dt2.NewRow()["Name"] = "Tandulkar");
dt2.Rows.Add(dt2.NewRow()["Name"] = "Amir");
dt2.Rows.Add(dt2.NewRow()["Name"] = "Sheqwag");

 DataTable dtUnion = dt1.AsEnumerable()
  .Union(dt2.AsEnumerable()).CopyToDataTable<DataRow>();
  • 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-31T00:52:17+00:00Added an answer on May 31, 2026 at 12:52 am

    The problem here is that Linq does not know that you want to compare the Name. Instead it does what it does for all object types it compares the hash which is different for two different instances.

    What you need todo is tell the Union method how to compare two items. You can do so by creating a custom IEqualityComparer that does compare two data rows the way you want it.

    Here is a sample implementation:

    class CustomComparer : IEqualityComparer<DataRow>
    {
        #region IEqualityComparer<DataRow> Members
    
        public bool Equals(DataRow x, DataRow y)
        {
            return ((string)x["Name"]).Equals((string)y["Name"]);
        }
    
        public int GetHashCode(DataRow obj)
        {
            return ((string)obj["Name"]).GetHashCode();
        }
    
        #endregion
    }
    

    When calling Union you then need to pass in an instance of this comparer:

    var comparer = new CustomComparer();
    DataTable dtUnion = dt1.AsEnumerable()
          .Union(dt2.AsEnumerable(), comparer).CopyToDataTable<DataRow>();
    

    See here for more info:
    http://msdn.microsoft.com/en-us/library/bb358407.aspx

    Word of advice:
    Linq is best with customized data classes, which DataRow is not . It’s best to have an actual Name property on the class, only then Linq can really shine.
    If you don’t need the flexibility of dynamic schema you should stay away from DataTable and implement custom classes that resemble exactly what you need, since DataTable is extremely bloated and slow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could somebody please name a few. I could given time, but this is for
yes, this is a homework-type question but could you please help me out? In
Please could someone post an example of how to check if an element exists
I'm a little lost in general, but if someone could please briefly explain why
could someone please help me with this problem, sorry for the Language in the
Could you please show me the C# Equivalent of this VB.NET code: Public Partial
please check the following code. $imagebaseurl = 'support/content_editor/uploads/$name'; The $imagebaseurl is a variable that
Could you please tell me if there is any equivalent of Access' DISTINCTROW for
Could somebody please do a rundown of how to programmatically encrypt a config-file in
Could someone please demystify interfaces for me or point me to some good examples?

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.