How to find Duplicates Elements with LINQ

No.of Views3244
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  05 Mar 2011 05:03:59
Tag : LINQ , How to
In this snippet I am going to discuss about how to get list of duplicate items for the collection that we do in sql.
emailbookmark add commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this snippet I am going to discuss about how to get list of duplicate items for the collection that we do in sql.

Implementation

For example I have to get list of the email id which is get entered in user table more than one time.

SELECT email, 
  COUNT(email) AS NumOccurrences
FROM users
GROUP BY email
HAVING ( COUNT(email) > 1 )

Linq query work same as on the set of collection and which make use of count

DataClassesDataContext db = new DataClassesDataContext();
var duplicates = db.Users
    .GroupBy(i => i.emailid)
    .Where(g => g.Count() > 1)
    .Select(g => g.emailid);
foreach (var d in duplicates)
    Console.WriteLine(d);

So by using above query you can easily achieve task of finding duplicate for your set of collection.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Expert
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    Usage of Except Operator in LINQ
    Published Date : 28/May/2010
    Linq contains lots useful operators and i have found more two operators that can be help full in our day to day programming life. Here are explanation.
    Published Date : 21/Jun/2010
    Microsoft .NET framework 4.0 is having many features that make developers life very easy. Its also provides some enhancement to Linq also. I just found a great operator called Zip which merge the sequence of two entities.
    Published Date : 21/Jun/2010
    The following example shows how query execution is deferred until the results is enumerated.
    Published Date : 02/Sep/2010
    I got curious about the how well the LINQ implementation will perform and thought of writing this article.
    Published Date : 16/Apr/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top