C # Linq - duplicate string search

Person

Name              City

Joe         Houston   
Jerry       London    
Alex        Houston   
Jerry       London    

How to return a repeating string using LINQ, for example

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 };
+3
source share
2 answers

You need to not choose somewhere:

var qry =  from m in context.Collections
           group m by new { m.city, m.name } into grp
           where grp.Count() > 1
           select grp.Key;
+14
source

Set up @Jon Skeet's answer with the “one liner” alternative, which returns duplicate values, not just keys:

var duplicates = db.Collections.GroupBy(a => new { m.city, m.name })
                               .Where(a => a.Count() > 1)
                               .SelectMany(a => a.ToList());
0
source

Source: https://habr.com/ru/post/1725439/


All Articles