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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:45:33+00:00 2026-05-16T02:45:33+00:00

I am using C# + VS2008 + .Net + ASP.Net + IIS 7.0 +

  • 0

I am using C# + VS2008 + .Net + ASP.Net + IIS 7.0 + ADO.Net + SQL Server 2008. I have a ADO.Net datatable object, and I want to filter out duplicate/similar records (in my specific rule to judge whether records are duplicate/similar — if record/row has the same value for a string column, I will treat them as duplicate/similar records), and only keep one of such duplicate/similar records.

The output needs to be a datatable, may output the same datatable object if filter operation could be operated on the same datatable object.

What is the most efficient solution?

  • 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-16T02:45:34+00:00Added an answer on May 16, 2026 at 2:45 am

    Are you using .NET 3.5? If you cast your data rows, you can use LINQ to Objects:

    var distinctRows = table.Rows.Cast<DataRow>().Distinct(new E());
    
    ...
    
    public class E : IEqualityComparer<DataRow>
    {
        bool IEqualityComparer<DataRow>.Equals(DataRow x, DataRow y)
        {
            return x["colA"] == y["colA"];
        }
    
        int IEqualityComparer<DataRow>.GetHashCode(DataRow obj)
        {
            return obj["colA"].GetHashCode();
        }
    }
    

    Or an even simpler way, since you’re basing it on a single column’s values:

    var distinct = from r in table.Rows.Cast<DataRow>()
                   group r by (string)r["colA"] into g
                   select g.First();
    

    If you need to make a new DataTable out of these distinct rows, you can do this:

    var t2 = new DataTable();
    t2.Columns.AddRange(table.Columns.Cast<DataColumn>().ToArray());
    foreach(var r in distinct)
    {
        t2.Rows.Add(r);
    }
    

    Or if it would be more handy to work with business objects, you can do an easy conversion:

    var persons = (from r in distinct
                   select new PersonInfo
                   {
                       EmpId = (string)r["colA"],
                       FirstName = (string)r["colB"],
                       LastName = (string)r["colC"],
                   }).ToList();
    
    ...
    
    public class PersonInfo
    {
        public string EmpId {get;set;}
        public string FirstName {get;set;}
        public string LastName {get;set;}
    }
    

    Update

    Everything you can do in LINQ to Objects can also be done without it: it just takes more code. For example:

    var table = new DataTable();
    var rowSet = new HashSet<DataRow>(new E());
    var newTable = new DataTable();
    foreach(DataColumn column in table.Columns)
    {
        newTable.Columns.Add(column);
    }
    foreach(DataRow row in table.Rows)
    {
        if(!rowSet.Contains(row))
        {
            rowSet.Add(row);
            newTable.Rows.Add(row);
        }
    }
    

    You could also use a similar strategy to simply remove duplicate rows from the original table instead of creating a new table.

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

Sidebar

Related Questions

EDIT: I am using VS2008, .NET 3.5 I have a DataTable, which is populated
I have an app using the ADO.NET entity framework (the VS2008 version, not the
I am using C# + VS2008 + .Net 3.5 + ASP.Net + IIS 7.0
I am using VS2008, and ASP.NET 3.5 C# I have an array list that
I have a arraylist in my web application project in asp.net/C#/VS2008 and I'm using
I have an Asp.net MVC2 web application. I built the application using VS2008 and
i am using asp.net c# jquery vs 2008 I have two check box list
I am using VS2008 + C# + .Net 3.5 + IIS 7.0 + ASP.Net
I have developed an ASP.Net 2.0 website (Using VS 2008). The website uses sqlmembershipprovider
I am developing a web application using ASP .NET 2.0, VS 2008 and SQL

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.