如何在不使用 model.Count 的情况下获取 Viewbag 的计数

How do I get the count of a Viewbag without using model.Count

如何从 veiwbag 中的 lambda 表达式中获取项目数?

我不想使用 model.count(),因为我有一个针对其他模型的模型指令,而这是不同的东西

这是我的代码

            var Count = _context.Users_Accounts_Address
         .Where(c => c.Email == user)
         .Select(c =>  c.Post_Code + " " +c.AddressType );

 ViewBag.ReturnCount = Count.CountAsync();

我的观点

@ViewBag.ReturnCount  

在运行时但是我回来了

 System.Threading.Tasks.Task`1[System.Int32]

当您调用 .CountAsync() 时,您会返回一个异步 Task<T> 对象(在本例中 T 是一个 int,因为它是 return 类型非异步 .Count() 方法。

您应该使用:

ViewBag.ReturnCount = Count.Count();

ViewBag.ReturnCount = await Count.CountAsync(); 

(如果你的控制器是异步的)