Exemple #1
0
        /**
         * Вспомогательный метод получения списка должностей
         */
        private ApiResult GetList(JobTitle filters)
        {
            ApiResult result = new ApiResult();

            try
            {
                IQueryable <JobTitle> list = DataBase.JobTitles;

                // Фильтрация по параметрам из строки запроса
                Type   jtType       = new JobTitle().GetType();
                Type[] exprArgTypes = { list.ElementType };
                foreach (PropertyInfo index in jtType.GetProperties())
                {
                    // свойство не виртуальное и пришло значение в форме
                    if (!jtType.GetProperty(index.Name).GetGetMethod().IsVirtual&& Request.Params.Get(index.Name) != null)
                    {
                        ParameterExpression  p          = Expression.Parameter(typeof(JobTitle), "p");
                        MemberExpression     member     = Expression.PropertyOrField(p, index.Name);
                        LambdaExpression     lambda     = Expression.Lambda <Func <JobTitle, bool> >(Expression.Equal(member, Expression.Constant(filters.GetType().GetProperty(index.Name).GetValue(filters))), p);
                        MethodCallExpression methodCall = Expression.Call(typeof(Queryable), "Where", exprArgTypes, list.Expression, lambda);
                        list = (IQueryable <JobTitle>)list.Provider.CreateQuery(methodCall);
                    }
                }

                result.DataSet = list.Select(jobtitle => new
                {
                    Id         = jobtitle.Id,
                    Title      = jobtitle.Title,
                    Deleted    = jobtitle.Deleted,
                    UsersCount = jobtitle.Users.Count()
                });
                result.Success = true;
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Message = e.ToString();
            }
            return(result);
        }