获取linq查询上下文之外的count变量

时间:2017-03-01 13:06:49

标签: c# linq

如何将计数分配给totalCases变量?

我只是想知道我是否可以做以下事情,

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

使用let声明的变量是否可以在查询的上下文之外访问?

 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
                };
            }

1 个答案:

答案 0 :(得分:0)

删除let行,同时删除totalCases

在您返回数据时,只需致电Count(),将谓词传入您的列表。

I.E

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