2

I have a table in my database.

`tblCustomers`
    - CustomerId 
    - CustomerName 

Now my need is to get all the customers grouped by CustomerName but at least there should more than one occurrence of the customername.

Like if there are 10 Customers with name = "ABC", get them into one array but 1 customer with name = "XYZ"(Only one record with this name) Don't get this.

This is what I was thinking

var query = (from c in entities.tblCustomers
group c.CustomerName into cust 
//where cust.Key.Count >=2  (how to get this)
select ABC()
{
});
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Possible duplicate of [How to get duplicate items from a list using LINQ?](http://stackoverflow.com/questions/3811464/how-to-get-duplicate-items-from-a-list-using-linq) – Marco Jan 22 '17 at 09:45

2 Answers2

2

It could be like this:

from c in entities.tblCustomers
group c by c.CustomerName into cust 
where cust.Count() > 1
select cust.Key

It equivalent to SQL HAVING Clause.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
2

With method operators, it would be like this:

entities.tblCustomers
    .GroupBy(c => c.CustomerName)
    .Where(grp => grp.Count() > 1)
    .Select(grp => grp.Key);
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64