2017-03-01 54 views
0

如何將count分配給totalCases變量?在linq查詢上下文之外獲取count變量

我只是想知道我是否可以做一些像下面,

let count = DbContext.Set<CaseAction>().Count(c => c.IsToDo == isToDo) into totalCases 

是讓聲明的變量可以查詢的範圍之外進行訪問?

public PagedList GetCatTaskByCompanyId(int companyId, int? isToDo, int pageIndex, int pageSize) 
      { 
       var totalCases = 200; 
       var caseModels = (from casemodel in DbContext.Set<CaseAction>() 
            where casemodel.IsToDo == isToDo 
            let count = DbContext.Set<CaseAction>().Count(c => c.IsToDo == isToDo) 
            orderby casemodel.TaskCreatedOn descending 
            select new CaseDetailsModel 
            { 
             TaskId = casemodel.Id, 
             CompanyId = companyId, 
             Title = casemodel.Title, 
             TaskTypeId = casemodel.TaskTypeId, 
             TaskTypeGroupId = casemodel.TaskTypeGroupId, 
             CaseSubject = casemodel.TaskName, 
             CurrentComment = casemodel.CurrentComment, 
             IsPinned = casemodel.Pinned, 
             CurrentActionTypeId = casemodel.CurrentActionTypeId, 
             CurrentActionTypeName = casemodel.CurrentActionTypeName, 
             NextActionDueOn = casemodel.NextActionDueOn, 
             CurrentActionCreatedOn = casemodel.CurrentActionCreatedOn, 
             CurrentActionAssignedTo = casemodel.AssignedToStaffName, 
             NextActionTypeName = casemodel.NextActionTypeName, 
             ActionCount = casemodel.ActionCount 
            }).Skip(pageIndex * pageSize) 
            .Take(pageSize) 
            .ToList(); 

       return new PagedList 
       { 
        Data = caseModels, 
        PageIndex = pageIndex, 
        PageSize = pageSize, 
        Count = totalCases 
       }; 
      } 

回答

0

取出let線,也拆除totalCases

當您返回數據時,只需將您的謂詞傳入Count()即可。

return new PagedList 
       { 
        Data = caseModels, 
        PageIndex = pageIndex, 
        PageSize = pageSize, 
        Count = caseModels.Count(c => c.IsToDo == isToDo) 
       };