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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:30:58+00:00 2026-06-16T19:30:58+00:00

I have the following datatable – Let’s say called MyTable : Col1: Col2: Col3:

  • 0

I have the following datatable – Let’s say called MyTable:

Col1:    Col2:    Col3:    Col4:
1        abc      def      <null>
2        abc      def      ghi
1        abc      def      <null>
3        abc      def      <null>
1        abc      def      <null>

And I’m trying to get the distinct rows:

Col1:    Col2:    Col3:    Col4:
1        abc      def      <null>
2        abc      def      ghi
3        abc      def      <null>

I tried the following LINQ statement:

MyTable = (From dr As DataRow In MyTable Select dr).Distinct.CopyToDataTable

But it’s returning the original datatable with the repeated rows back to me.

What am I doing wrong AND how can I get my desired output??

  • 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-16T19:30:59+00:00Added an answer on June 16, 2026 at 7:30 pm

    Distinct relies on the objects all implementing IEquatable and/or having sensible implementations of GetHashCode and Equals. The DataRow class…doesn’t. It doesn’t check that the value of each column is equal in the Equals method, it just uses the default implementation, which is to say that it checks that the references are equal, and that’s not what you want.

    You can provide your own IEqualityComparer, since you can’t change the methods in DataRow.

    The following should work, if you provide an instance of it to to Distinct:

    public class DataRowComparer : IEqualityComparer<DataRow>
    {
        public bool Equals(DataRow x, DataRow y)
        {
            for (int i = 0; i < x.Table.Columns.Count; i++)
            {
                if (!object.Equals(x[i], y[i]))
                    return false;
            }
            return true;
        }
    
        public int GetHashCode(DataRow obj)
        {
            unchecked
            {
                int output = 23;
                for (int i = 0; i < obj.Table.Columns.Count; i++)
                {
                    output += 19 * obj[i].GetHashCode();
                }
                return output;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following DataTable dt = new DataTable(); dt.Columns.Add(col1, typeof(string)); dt.Columns.Add(col2, typeof(string)); dt.Rows.Add(1,
I have the following code in a project I'm working on: foreach(DataTable myTable in
I have the following code that serializes a DataTable to XML. StringWriter sw =
I have converted my Datatable to json string use the following method... public string
I have a DataTable which is bound to datagridview (Winforms)... I use the following
I have following function - public static DataTable getDetails(PersonContext context) { DataTable dt =
I have the following jquery to setup my dataTable however the problem I run
i have the following class that inherits from DataTable: public class ExcelStaticDataTable : DataTable
I have the following jsfiddle that generates a YUI Datatable with checkboxes, but i
I have following function that converts normal DataTable to Pivot Table. But I have

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.