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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:18:25+00:00 2026-05-28T07:18:25+00:00

Here is if my DataTable DataTable dt = new DataTable(); dt.Rows.Add(2,Test1,Sample1); dt.Rows.Add(2,Test2,Sample2); dt.Rows.Add(4,Test3,Sample3); dt.Rows.Add(4,Test4,Sample4);

  • 0

Here is if my DataTable

DataTable dt = new DataTable();
dt.Rows.Add(2,Test1,Sample1);
dt.Rows.Add(2,Test2,Sample2);
dt.Rows.Add(4,Test3,Sample3);
dt.Rows.Add(4,Test4,Sample4);
dt.Rows.Add(2,Test5,Sample5);

I want to display message Duplicate record exist for class 4
it is possbile by using two loops by comparing, but i want an optimized code that will return me the duplicate record and i will display a message. The code may be using Linq if any body know please share..?

  • 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-28T07:18:26+00:00Added an answer on May 28, 2026 at 7:18 am

    Assuming that you get a duplicate record when the value of the first column is identical for two or more rows:

    var duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1);
    

    Here is an example:

    DataTable dt = new DataTable();
    dt.Columns.Add();
    dt.Columns.Add();
    dt.Columns.Add();
    dt.Rows.Add(1, "Test1", "Sample1");
    dt.Rows.Add(2, "Test2", "Sample2");
    dt.Rows.Add(3, "Test3", "Sample3");
    dt.Rows.Add(4, "Test4", "Sample4");
    dt.Rows.Add(5, "Test5", "Sample5");
    
    var duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1).ToList();
    Console.WriteLine("Duplicate found: {0}", duplicates.Any());
    
    dt.Rows.Add(1, "Test6", "Sample6");  // Duplicate on 1
    dt.Rows.Add(1, "Test6", "Sample6");  // Duplicate on 1
    dt.Rows.Add(3, "Test6", "Sample6");  // Duplicate on 3
    dt.Rows.Add(5, "Test6", "Sample6");  // Duplicate on 5
    
    duplicates = dt.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1).ToList();
    if (duplicates.Any())
        Console.WriteLine("Duplicate found for Classes: {0}", String.Join(", ", duplicates.Select(dupl => dupl.Key)));
    
    Console.ReadLine();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my datatable: DataTable CSVFile = new DataTable(); CSVFile.Columns.Add(Occurrence_Date, typeof(DateTime)); CSVFile.Columns.Add(Preanalytical_Before_Testing, typeof(string)); CSVFile.Columns.Add(Cup_Type,
Here is my code Dim dt As DataTable = New DataTable(Intervenant) For Each column
I am new in C#.I want to add rows in a GridView in runtime.
c#/oop noob here. I am creating a datatable from the schema of a sql
I am trying to convert all DateTime values in a DataTable to strings. Here
I use this: http://datatables.net/release-datatables/examples/api/select_row.html Here is function return selcted rows: function fnGetSelected( oTableLocal )
i have a datatable i created below i need to list all rows' cell
I have a DataTable which has some rows and I am using the select
Update: Here's a similar question Suppose I have a DataTable with a few thousand
Ok. So I am pretty new a this. I have a datatable that I

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.