该名称在当前上下文中不存在(在带有MVC实体框架的ActionResult中)

时间:2015-06-29 11:42:11

标签: asp.net-mvc entity-framework entity-framework-5

在ActionResult"详细信息(int?id)"我有一个控制器(在ActionResult中这是一个条件:if(User.Identity.IsAuthenticated){bla,bla):

            var TheUser = db.AspNetUsers.Where(u => u.Id == CurrentUser)
                         .Select(u => new
                         {
                             ID = u.Id,
                             Email = u.Email,
                             Username = u.UserName,
                             Surrname = u.Surname,
                             Name = u.Name,
                             Role = u.Role,
                             CreditBalance = u.CreditBalance
                         }).Single();

            var TheJournal = db.CreditJournal.Where(tj => tj.CvID == id && tj.UseBy == CurrentUser)
                            .Select(tj => new
                               {
                                   IdJournal = tj.IdJournal,
                                   Operation = tj.Operation,
                                   CvID = tj.CvID,
                                   CreditConsumed = tj.CreditConsumed,
                                   UseDate = tj.UseDate,
                                   UseBy = tj.UseBy
                               });

            var Counter = TheJournal.Count();

当我在调试模式中评估值时,我有:

TheUser |>>>    { ID = "56cc2430-4db5-4799-ad41-fa1d103d1967", Email = "sales@maps4u.ro", Username = "sales@maps4u.ro", Surrname = "Laurentiu", Name = "LAZAR", Role = 3, CreditBalance = 75 }  <Anonymous Type>

TheJournal |>>> {System.Data.Entity.Infrastructure.DbQuery<<>f__AnonymousType9<int,string,int?,int?,System.DateTime?,string>>}  System.Linq.IQueryable<<>f__AnonymousType9<int,string,int?,int?,System.DateTime?,string>> {System.Data.Entity.Infrastructure.DbQuery<<>f__AnonymousType9<int,string,int?,int?,System.DateTime?,string>>}

Counter |>>>    The name 'Counter' does not exist in the current context

上面显示的简单代码有什么问题? (TheJornal的等效SQL语句返回给定条件,至少4条记录)。 不知何故,我认为声明变量超出条件,但它们必须是什么类型? (无论如何第一个,TheUser就是好的,问题从第二个,TheJournal开始)

1 个答案:

答案 0 :(得分:1)

使用.ToList()进行TheJournal查询。然后使用Count来获得计数器 -

var Counter = TheJournal.Count