Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding duplicate row using Linq

Tags:

c#

linq

Person

Name        City

Joe         Houston   
Jerry       London    
Alex        Houston   
Jerry       London    

How to return duplicate row using LINQ like

Sql

SELECT name, city, count(*)
FROM collection
GROUP BY name,city 
HAVING count(*) > 1

I tried something

var qry =
    from m in context.Collections
    group m by new { m.city, m.name } into grp
    select new { rp = grp.Count() > 2 };            
like image 868
user215675 Avatar asked Jan 27 '26 21:01

user215675


1 Answers

You need a where, not a select:

var qry =  from m in context.Collections
           group m by new { m.city, m.name } into grp
           where grp.Count() > 1
           select grp.Key;
like image 117
Jon Skeet Avatar answered Jan 29 '26 10:01

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!