Ejemplo n.º 1
0
        public List <TModel> SelectGridDataForGridModel <TEntity, TModel>(
            IQueryable queryableSource, List <FilterItem> searchItems, int pageIndex, out int pageCount)
            where TEntity : class
        {
            pageCount = (((IQueryable <TEntity>)queryableSource).Count() + this.pageSize - 1) / this.pageSize;

            return
                (((IQueryable <TEntity>)queryableSource).OrderBy(
                     LambdaExpressionGenerator.GenerateOrderByExpression <TEntity>())
                 .Skip(this.pageSize * pageIndex)
                 .Take(this.pageSize)
                 .Select(
                     LambdaExpressionGenerator.GetSelectClause <TEntity, TModel>())
                 .ToList());
        }
Ejemplo n.º 2
0
        private static IQueryable <TEntity> GetRawQueryable <TEntity>(
            List <FilterItem> searchItems, bool isCommon)
            where TEntity : class
        {
            TAS_DevEntities1     dbContext = new TAS_DevEntities1();
            IQueryable <TEntity> queryable = dbContext.Set <TEntity>();

            if (!isCommon)
            {
                queryable =
                    queryable.Where(LambdaExpressionGenerator.GenerateWhereClause <TEntity>(searchItems));
            }

            return(queryable);
        }
Ejemplo n.º 3
0
        public static Expression AssembleContainsExpression <TEntity, TProperty>(
            ParameterExpression appendantParameter,
            Expression appendantExpression,
            FilterItem searchItem,
            List <string> propertyPath,
            ConstantExpression filterValues,
            Expression expressionBody)
        {
            ParameterExpression parameter = appendantParameter;
            Type type = typeof(TEntity);

            string collectionProperty = null;

            if (propertyPath.Count > 1)
            {
                type =
                    Expression.Property(appendantParameter, typeof(TEntity), propertyPath[0])
                    .Type.GetGenericArguments()
                    .First();

                collectionProperty = propertyPath.First();
                propertyPath.RemoveAt(0);

                parameter = Expression.Parameter(type, "collectionEntity");
            }

            Expression property =
                LambdaExpressionGenerator.AssembleProperty(
                    Expression.MakeMemberAccess(parameter, type.GetProperty(propertyPath.First())),
                    propertyPath.Skip(1).ToList());

            List <TProperty> array =
                searchItem.SearchData.Split(new[] { "," }, StringSplitOptions.None)
                .Select(s => (TProperty)Convert.ChangeType(s, typeof(TProperty)))
                .ToList();

            if (property.Type == typeof(int?))
            {
                property = Expression.Property(property, "Value");
            }

            Type searchValuesType = array.GetType().GetGenericArguments().FirstOrDefault();

            filterValues = Expression.Constant(array, array.GetType());

            expressionBody = Expression.Call(
                typeof(Enumerable), "Contains", new[] { searchValuesType }, filterValues, property);

            if (!string.IsNullOrEmpty(collectionProperty))
            {
                property = Expression.Property(appendantParameter, typeof(TEntity), collectionProperty);

                // inner where - where(w => array.contains(w.property))
                expressionBody = Expression.Call(
                    typeof(Enumerable),
                    "Where",
                    new[] { type },
                    property,
                    Expression.Lambda(expressionBody, new[] { parameter }));

                // any - where(w => array.contains(w.property)).Any()
                expressionBody = Expression.Call(typeof(Enumerable), "Any", new[] { type }, expressionBody);
            }

            return(expressionBody);
        }