JasonTheLucky
JasonTheLucky

Reputation: 187

LINQ using Group with Count and Where, easy SQL, harder in LINQ

I'm trying to display cities names where a count is greater than 1. I can do it easy in SQL and am close in LINQ but can't figure out how to use group and also get a count and display a name

        var query = (from c in Consumer
                   group c
                   by new { c.City, size = c.City.Count() }
                       into results
                       select new { Name = results.Key.City })
                    .Where(a => size > 0);

The size part doesn't work

Upvotes: 2

Views: 114

Answers (1)

Manish Mishra
Manish Mishra

Reputation: 12375

try this query:

var list= Consumer.GroupBy(s=>s.City)
              .Select(s=>new {
                          City = s.Key,
                          size = s.Count(),
                   })
              .Where(s=>s.size>0).ToList();

Upvotes: 4

Related Questions