Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by, Count and Lambda Expression

Tags:

I am trying to translate the following query:

SELECT STATE, COUNT(*) FROM MYTABLE GROUP BY STATE; 

Into a lambda expression. I am using C# and EntityFramework, however it doesnt seem I can make it work. Here is what I have on my respository so far:

public IEnumerable<object> PorcentajeState(Guid id) {     return _context.Sates.Where(a => a.Id == id)                          .GroupBy(a => a.State)                          .Select(n => new { n.StateId , n.Count() }); } 

Of course it doesnt compile and I am lost after googling for 2 hours . Could you please help me?

thanks in advance

like image 319
N8K8 Avatar asked Oct 10 '13 01:10

N8K8


People also ask

Can lambda expressions contain multiple statements?

In particular, a lambda function has the following characteristics: It can only contain expressions and can't include statements in its body.

What is lambda in SQL?

Lambda functions allow you to apply user-defined functions on arrays. The function iterates through the elements of an array and applies the user-provided transformation/expression. The function can take any number of array parameters. The expression doesn't have to be stored or defined before using it.


2 Answers

There are two issues here:

  1. The result of GroupBy will will be an enumerable of type IEnumerable<IGrouping<TKey, TSource>>. The IGrouping interface only has one property you can access, Key which is the key you specified in the GroupBy expression, and implements IEnumerable<T> so you can do other Linq operations on the result.
  2. You need to specify a property name for the anonymous type if it cannot be inferred from a property or field expression. In this case, you're calling Count on the IGrouping, so you need to specify a name for that property.

Try this:

public IEnumerable<object> PorcentajeState(Guid id) {     return _context.Sates.Where(a => a.Id == id)                          .GroupBy(a => a.StateId)                          .Select(g => new { g.Key, Count = g.Count() }); } 

The equivalent in query syntax would be

public IEnumerable<object> PorcentajeState(Guid id) {     return from a in _context.Sates            where a.Id == id            group a by a.StateId into g            select new { a.Key, Count = g.Count() }; } 

In either case, if you want the first property to be named StateId instead of Key, just change that to

new { StateId = g.Key, Count = g.Count() } 
like image 127
p.s.w.g Avatar answered Nov 07 '22 18:11

p.s.w.g


This one is good

public IEnumerable<object> PorcentajeState(Guid id)     {         return _context.Sates.Where(a => a.Id == id)                              .GroupBy(a => a.StateId)                              .Select(g => new { g.Key, Count = g.Count() });     } 

But try this.

public IEnumerable<object> PorcentajeState(Guid id)         {             return _context.Sates.Where(a => a.Id == id)                                  .GroupBy(a => a.StateId)                                  .Select(g => new { g.Key.StateId, Count = g.Count() });         } 
like image 20
Ahsan Qureshi Avatar answered Nov 07 '22 16:11

Ahsan Qureshi