"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Sunday, December 2, 2012

Count of duplicate record in dataset or datatable



Count of duplicate record in dataset or datatable in C# 


   DataTable dtList = dtduplicate();

        DataTable dt = ds.Tables[0];
      
        var result = from c in dt.AsEnumerable()
                     group c by new
                     {
                         ClientID = c.Field<string>("ClientID"),
                         CurrentServicerLoanNo = c.Field<string>("CurrentServicerLoanNo"),
                         CurrentLoanServicer=c.Field<string>("CurrentLoanServicer"),
                         NameOfOriginatingLender=c.Field<string>("NameOfOriginatingLender"),
                         OriginalLoanNumber=c.Field<string>("OriginalLoanNumber")
                     } into g
                     where g.Count() > 1
                     select new
                     {
                        
                         g.Key.ClientID,
                         g.Key.CurrentServicerLoanNo,
                         g.Key.CurrentLoanServicer,
                         g.Key.NameOfOriginatingLender,
                         g.Key.OriginalLoanNumber,
                         Noofrec = g.Count()
                     };

for (int i = 0; i < result.ToList().Count; i++)
        {
            var item = result.ToList()[i];
            //   resultTable.Rows.Add(item.ProdId, item.Pin, item.Noofrec);
            dtList.Rows.Add(null,item.ClientID, item.CurrentLoanServicer, item.CurrentServicerLoanNo, item.NameOfOriginatingLender, item.Noofrec, item.OriginalLoanNumber);
        }

   public static DataTable dtduplicate()
    {
        DataTable dtDuplicatelist = new DataTable();

        DataColumn auto = new DataColumn("AutoID", typeof(System.Int32));
        dtDuplicatelist.Columns.Add(auto);
        auto.AutoIncrement = true;
        auto.AutoIncrementSeed = 1;
        auto.ReadOnly = true;
        auto.Unique = true;


        dtDuplicatelist.Columns.Add("ClientID", typeof(string));
        dtDuplicatelist.Columns.Add("CurrentLoanServicer", typeof(string));
        dtDuplicatelist.Columns.Add("CurrentServicerLoanNo", typeof(string));
        dtDuplicatelist.Columns.Add("NameOfOriginatingLender", typeof(string));
        dtDuplicatelist.Columns.Add("Noofrec", typeof(string));
        dtDuplicatelist.Columns.Add("OriginalLoanNumber", typeof(string));

        dtDuplicatelist.PrimaryKey = new DataColumn[] { auto }; // Setting as primary key Then only it will add autoincrement to the Column
        return dtDuplicatelist;
    }

 



If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: