Using an expression lambda expression

I need to get the following output in a list, I am using MVC4 and C #. Basically, I have to fulfill the request:

SELECT ESTADO, COUNT(*)
FROM VISITAS
WHERE CICLOID=ID
GROUP BY ESTADO;

To achieve this, I wrote the following procedure in my repository:

  public List<object> PorcentajeVisitasCiclo(Guid id)
  {
      return new List<object> {_context.Visitas
          .Where(a => a.CicloId == id)
          .GroupBy(a => a.Estado)
          .Select(n => new { Text = n.Key.Descripcion , Value = n.Count() })};
  }

Could you tell me where I am wrong? It does not give any compilation error, but returns nothing

thanks in advance

+4
source share
4 answers

This may be an option. I had the same problem and returning an object list was not a solution (some LINQ error that I don't remember). I went for an easier solution.

public List<DummyModel> Method(int id)
    {
        return _context.Visitas.Where(a => a.CicloId == id).GroupBy(a => a.Estado).
            Select(n => new DummyModel { Name = n.Key.Descripcion, Value = n.Count() }).ToList();
    }
+4
source

List<object> , LINQ. , , .

LINQ, :

return _context.Visitas.Where(a => a.CicloId == id)
    .GroupBy(a => a.Estado)
    .Select(n => new { Text = n.Key.Descripcion , Value = n.Count() }).ToList();

, , , a List<yourAnonymousType> List<object>. , :

return _context.Visitas.Where(a => a.CicloId == id)
    .GroupBy(a => a.Estado)
    .Select(n => new { Text = n.Key.Descripcion , Value = n.Count() })
    .Cast<object>().ToList();

, YourDataType Text Value. List<YourDataType>.

+4

List<object> IEnumerable.

BTW, List<object> List.

ToList() . , yield return . , . ToList().

+1
return (_context.Visitas.Where(a => a.CicloId == id)
    .GroupBy(a => a.Estado)
    .Select(n => new { Text = n.Key , Value = n.Count() })).Cast<object>().ToList();
+1

All Articles