Group by, Count and Lambda Expression

后端 未结 2 660
半阙折子戏
半阙折子戏 2020-12-29 05:51

I am trying to translate the following query:

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

Into a lambda expression. I am using C# a

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-29 06:14

    This one is good

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

    提交回复
    热议问题