Thursday, March 3, 2011

Find duplicate with Linq

Shout it kick it on DotNetKicks.com
In this small post I am going to discuss about how to get list of duplicate items for the collection that we do in sql.
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.

1 comment:

  1. obviously like your web-site however you need to take a look at the spelling on several of your posts.
    Several of them are rife with spelling problems and I to find it very bothersome to tell the
    reality then again I'll certainly come back again.

    Here is my page ... where can i get a payday loans uk

    ReplyDelete