Linq with group by having count

Fernando picture Fernando · Jan 16, 2010 · Viewed 141k times · Source

how do I write this query in linq (vb.net)?

 select B.Name
 from Company B
 group by B.Name
 having COUNT(1) > 1

Answer

Thomas Levesque picture Thomas Levesque · Jan 16, 2010

Like this:

from c in db.Company
group c by c.Name into grp
where grp.Count() > 1
select grp.Key

Or, using the method syntax:

Company
    .GroupBy(c => c.Name)
    .Where(grp => grp.Count() > 1)
    .Select(grp => grp.Key);