Error while doing counter via C #

1

I'm trying to make a pendulum counter to my system, but it's giving error, at the time of calculating, could anyone help me?

You are giving error in .Count as you can see in the picture below.

MyController

publicasyncTask<ActionResult>Index(){if(!Cookies.Exists("hid"))
                return RedirectToAction("Index", "Login", new { area = "Entrar" });

            var hospitalId = int.Parse(Cookies.GetCookie("hid"));
            var listaDia1 = await Dia1Service.GetPendenciasByUser(hospitalId);
            var listaDia2 = await Dia2Service.GetPendenciasByUser(hospitalId);
            var listaDia3 = await Dia3Service.GetPendenciasByUser(hospitalId);
            var listaDia7 = await Dia7Service.GetPendenciasByUser(hospitalId);
            var lista90Dias = await Dados90DiasService.GetPendenciasByUser(hospitalId);
            var listaAlta = await AltaUTIService.GetPendenciasByUser(hospitalId);
            var listaDemografia = await DemografiaContatosService.GetPendenciasByUser(hospitalId);
            var listaDadosBasais = await DadosBasaisService.GetPendenciasByUser(hospitalId);

            ViewBag.listaD1 = listaDia1;
            ViewBag.listaD2 = listaDia2;
            ViewBag.listaD3 = listaDia3;
            ViewBag.listaD7 = listaDia7;
            ViewBag.lista90d = lista90Dias;
            ViewBag.listaAlta = listaAlta;
            ViewBag.listaDeC = listaDemografia;
            ViewBag.listaDb = listaDadosBasais;
            return View();
        }

My Contactor in PendenciaFilter.cs

void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            AtualizarNotificacoes(filterContext);
        }

        private void AtualizarNotificacoes(ActionExecutedContext filterContext)
        {
            if (string.IsNullOrEmpty(Cookies.GetCookie("uid"))) return;

            var hospitalId = int.Parse(Cookies.GetCookie("hid"));

            // Pegar todas as notificações deste usuario
            var altaUtiPendencias = new AltaUtiService().GetPendenciasByUser(hospitalId).Count();
            var dia1Pendencias = new Dia1Service().GetPendenciasByUser(hospitalId).ToString();
            filterContext.Controller.ViewBag.QtdPendencias = altaUtiPendencias + dia1Pendencias;
        }
    
asked by anonymous 27.03.2018 / 15:22

1 answer

3

Your repository returns a async Task<IEnumerable<object>> , so you need a await to access the collection (which has the Count method)

'(await new AltaUtiService().GetPendenciasByUser(hospitalId)).Count()'

I really hope your AltaUtiService.GetPendenciasByUser is not calling ToListAsync before returning the collection.

Quando usar Entity Framework com Repository Pattern?

EDIT

You should modify your filter so that it runs AtualizarNotificacoes synchronously.:

public override void OnActionExecuted (ActionExecutedContext filterContext)
{
    AtualizarNotificacoes(filterContext).GetAwaiter().GetResult();
}

private async Task AtualizarNotificacoes(ActionExecutedContext filterContext)
{
    if (string.IsNullOrEmpty(Cookies.GetCookie("uid"))) return;

    var hospitalId = int.Parse(Cookies.GetCookie("hid"));

    // Pegar todas as notificações deste usuario
    var altaUtiPendencias = (await new AltaUtiService().GetPendenciasByUser(hospitalId)).Count();
    var dia1Pendencias = (await new Dia1Service().GetPendenciasByUser(hospitalId)).ToString();
    filterContext.Controller.ViewBag.QtdPendencias = altaUtiPendencias + dia1Pendencias;
}
    
27.03.2018 / 15:34