Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Retrieve duplicate records from data table using LINQ

 Following code examples explains how to How to find duplicate record using Linq from DataTable.

Find duplicate record for single columns from Data Table using LINQ

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
            var duplicatesData = EmployeeData.AsEnumerable().GroupBy(r => r[0]).Where(gr => gr.Count() > 1);
//Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Retrieve duplicates for two columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records applying grouping on two columns.

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

Get duplicate records for multiple columns in data table using LINQ

Following linq query LINQ Example to retrieve duplicate records, applying grouping on three columns

     /// <summary>
        /// Returns duplicate records from Employee Table.
        /// </summary>
        private DataTable GetDuplicateEmployeeRecords(DataTable EmployeeData)
        {
            DataTable duplicates = null;
            if (EmployeeData!= null && EmployeeDat.AsEnumerable().Any())
            {
                var duplicatesData = EmployeeData.AsEnumerable().GroupBy(x => new
                {
                    EmployeeId= x.Field<int>("EmployeeId"),
                    CountryId = x.Field<int>("CountryId"),
                    City= x.Field<string>("City")
                })
                 .Where(gr => gr.Count() > 1).SelectMany(dupRec => dupRec);
 
                //Check if duplicate records exists
                if (duplicatesData != null &&
                    duplicatesData.Any())
                {
                    duplicates = duplicatesData.CopyToDataTable();
                }
            }
            return duplicates;
        }

 

managed code vs unmanaged code

In this article I will try to explain you managed code and unmanaged Code its differences, advantages and disadvantages.

Managed Code

Program/code written within your application domain (i.e. .Net Framework) is, managed code. The program that are within domain are faster.

In Detail:

The code which is written in .net Framework supported language called as managed code.  This code is directly executed by CLR with help of managed code execution. Any program that is written in .NET Framework is managed code.

Managed code uses CLR which to handle memory management, security, exception etc.

Advantages using Managed Code:

1.  Memory management
2.  Thread management
3.  Exception handling
4.  Garbage collection
5.  Security
6.  Base Library for code reusability.
7.  Able to write code in multiple languages.
8.  Performance improvement.
9. Type safety and security.   
10.  Re-usability of Code

Disadvantages:

1.  New and changing technology it takes time for people to learn that technology
2.  Multi-platform support isn't available from MS and isn't available straight after installing Visual Studio, but with VS 2015 supporting Multi-platform.
3.   Managed code can be slower than native code
4.  NET framework is free to download but Code Editor is costly.
5.  Not suitable for High End Application

Managed Code example:

Following C# code is managed code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
 
namespace C4RandDWebApp
{
   public class ProductDataAccess
   {
      private string conString = "Data Source=.\\;Integrated Security=true;Initial Catalog=WebShop";
 
      /// <summary>
      /// Get product Details with dataset
      /// </summary>
      Public DataSet GetProductDetails()
      {
         var con = new SqlConnection(conString);
         var da = new SqlDataAdapter("GetProductDetails", con)
         {
            SelectCommand =
            {
               CommandType = CommandType.StoredProcedure
            }
         };
         var ds = new DataSet();
         da.Fill(ds, "Products");
         return ds;
      }
}

 

Unmanaged Code

The program, code written in language that is not supported by .net Framework called as Unmanaged code.  

Code written in C language or C++ which is not supported by .net framework is unmanaged code.

In Detail:

Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C,C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with code of VB, ASP and COM are examples of unmanaged code.

Unmanaged code is executed with help of wrapper classes.

 There are two types of Wrapper classes:

1.  CCW (COM Callable Wrapper)
2.  RCW (Runtime Callable Wrapper).

Advantages using:

1.  Easy to learn and write code.
2.  You can execute code in any platform with some helper classes (for example Wrapper classes).
3.  Native code should be faster and have a smaller memory footprint than managed code, reason lot of things are happening on behind with Garbage collection, Type safety.
4. Maintaining native code is easy compared with managed code.

Disadvantages using:

1.  You may face memory leak and that leads to major performance issues. 
2.  Handling explicitly memory management.
 3.  Type safety, array bound and index checking, for example variable data type casting from one to other is hard in unmanaged code.
4.  No default support for exception handling, you require to explicitly handle exceptions.
5. Code security issues.

Unmanaged code example:

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
              char c,myfname[10];
              ofstream out;
              cout<<"Enter File name:";
              cin>> myfname;
              out.open(myfname);
              cout<<"Enter data that’s is to be stored in file (Enter # at end):\n";
              while((c=getchar())!='#')
              {
                            out<<c;
              }
              out.close();
              getch();
}

Choosing managed or unmanaged code:

It is better to use hardware related application using native code.
For Web application, Finance applications better to use managed code.