How do I get the count of items from a lambda expression in a veiwbag ?
I dont want to use model.count() as I have a model directive for other models and this is for something different
here is my code
var Count = _context.Users_Accounts_Address
.Where(c => c.Email == user)
.Select(c => c.Post_Code + " " +c.AddressType );
ViewBag.ReturnCount = Count.CountAsync();
and my view I put
@ViewBag.ReturnCount
At runtime However I get back
System.Threading.Tasks.Task`1[System.Int32]
When you call .CountAsync()
you get back an asynchronous Task<T>
object (in this case T
is an int
as that's the return type of the non-async .Count()
method.
You should use either:
ViewBag.ReturnCount = Count.Count();
or
ViewBag.ReturnCount = await Count.CountAsync();
(if your controller is async)