9

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 a message "Duplicate record exists for class 4"

It's possible by comparing using two loops, but I want optimized code that will return the duplicate record and display a message. The solution may use LINQ.

1
  • 2
    What is "class 4"? When do you have a duplicate? Commented Jan 20, 2012 at 11:33

2 Answers 2

30

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();
Sign up to request clarification or add additional context in comments.

2 Comments

If AsEnumerable() is missing, add a reference to System.Data.DataTableExtensions
this works pretty nice. How would I put the results of 'duplicates' into a DataTable so that I can prompt the user of each discrepancy and handle the result of their choice?
3

You can get Intersect of two LINQ query or Results. Intersect means find common records between two LINQ results.

Here is example for this.

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataColumn dc;
    DataRow dr;
    ds.DataSetName = "products";
    dt.TableName = "product";

    dc = new DataColumn("product_id",long.MaxValue.GetType());
    dt.Columns.Add(dc);

    dc = new DataColumn("product_name");
    dt.Columns.Add(dc);

    dr = dt.NewRow();
    dr["product_id"] = 1;
    dr["product_name"] = "Monitor";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 2;
    dr["product_name"] = "Mouse";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 3;
    dr["product_name"] = "KeyBoard";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["product_id"] = 4;
    dr["product_name"] = "LCD";
    dt.Rows.Add(dr);

    ds.Tables.Add(dt);

    IEnumerable<DataRow> objResult1 = from tbl in dt.AsEnumerable()
                                   where tbl.Field<long>(0) >=3
                                   select tbl;



    IEnumerable<DataRow> objResult2 = from tbl in ds.Tables[0].AsEnumerable()
                                   let product_name = tbl.Field<string>(1)
                                   where product_name.StartsWith("Key")
                                   || product_name.StartsWith("Mo")
                                   select tbl;




    IEnumerable<DataRow> objUnionResult = objResult1.Intersect(objResult2);

    Response.Write("<br/><br/><b>Intersect Query Results</b>");
    foreach (DataRow row in objUnionResult)
    {
        Response.Write(string.Format("<br/>Product ID: {0} ,  Product Name: {1}", row.Field<long>(0), row.Field<string>(1)));
    }
    Response.Write("<br/><br/>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.