コード例 #1
0
        /// <summary>
        /// Binds the grid.
        /// </summary>
        private void BindGrid()
        {
            var dataContext = new RockContext();

            var stepProgramsQry = new StepProgramService(dataContext)
                                  .Queryable();

            // Filter by: Category
            if (_categoryGuids.Any())
            {
                stepProgramsQry = stepProgramsQry.Where(a => a.Category != null && _categoryGuids.Contains(a.Category.Guid));
            }
            else
            {
                var categoryId = rFilter.GetUserPreference("Category").AsIntegerOrNull();

                if (categoryId.HasValue && categoryId > 0)
                {
                    stepProgramsQry = stepProgramsQry.Where(a => a.CategoryId == categoryId.Value);
                }
            }

            // Filter by: Active
            var activeFilter = rFilter.GetUserPreference("Active").ToLower();

            switch (activeFilter)
            {
            case "active":
                stepProgramsQry = stepProgramsQry.Where(a => a.IsActive);
                break;

            case "inactive":
                stepProgramsQry = stepProgramsQry.Where(a => !a.IsActive);
                break;
            }

            // Sort by: Order
            stepProgramsQry = stepProgramsQry.OrderBy(b => b.Order);

            // Retrieve the Step Program data models and create corresponding view models to display in the grid.
            var stepService = new StepService(dataContext);

            var completedStepsQry = stepService.Queryable().Where(x => x.StepStatus != null && x.StepStatus.IsCompleteStatus);

            var stepPrograms = stepProgramsQry.Select(x =>
                                                      new StepProgramListItemViewModel
            {
                Id                 = x.Id,
                Name               = x.Name,
                IconCssClass       = x.IconCssClass,
                Category           = x.Category.Name,
                StepTypeCount      = x.StepTypes.Count,
                StepCompletedCount = completedStepsQry.Count(y => y.StepType.StepProgramId == x.Id)
            })
                               .ToList();

            gStepProgram.DataSource = stepPrograms;

            gStepProgram.DataBind();
        }