예제 #1
0
        public static IQueryable <TEntity> AutoWhere <TEntity>(this IQueryable <TEntity> query, PageCommand Cmd)
            where TEntity : class
        {
            Expression <Func <TEntity, bool> > where = null;
            var param = Expression.Parameter(typeof(TEntity), "x");

            if (!string.IsNullOrEmpty(Cmd.Keyword))
            {
                Expression <Func <TEntity, bool> > searchWhere = null;
                var words = Cmd.Keyword.Split(' ').Where(x => !string.IsNullOrEmpty(x));
                if (Cmd.KeywordFileds == null)
                {
                    var        type     = typeof(TEntity);
                    Expression keyWhere = null;
                    foreach (var prop in type.GetProperties())
                    {
                        var left = Expression.PropertyOrField(param, prop.Name);

                        foreach (var word in words)
                        {
                            if (left.Type != typeof(string))
                            {
                                var converter = TypeDescriptor.GetConverter(left.Type);
                                if (converter != null)
                                {
                                    try
                                    {
                                        dynamic val   = converter.ConvertFromString(word);
                                        var     right = Expression.Constant(val, left.Type);
                                        var     exp   = Expression.Equal(left, right);

                                        if (keyWhere == null)
                                        {
                                            keyWhere = exp;
                                        }
                                        else
                                        {
                                            keyWhere = Expression.Or(keyWhere, exp);
                                        }
                                    }
                                    catch (Exception e) { var msg = e.Message; }
                                }
                            }
                            else
                            {
                                var right  = Expression.Constant(word, left.Type);
                                var method = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
                                var exp    = Expression.Call(left, method, right);

                                if (keyWhere == null)
                                {
                                    keyWhere = exp;
                                }
                                else
                                {
                                    keyWhere = Expression.Or(keyWhere, exp);
                                }
                            }
                        }

                        if (keyWhere != null)
                        {
                            var lambda1 = Expression.Lambda <Func <TEntity, bool> >(keyWhere, param);
                            searchWhere = searchWhere.And(lambda1);
                        }
                    }
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(keyWhere, param);

                    if (searchWhere != null)
                    {
                        where = where.And(searchWhere);
                    }
                }

                //if (searchWhere != null) where = where.And(searchWhere);
            }

            if (Cmd.Eq != null)
            {
                foreach (var prop in Cmd.Eq.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var exp    = Expression.Equal(left, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.Like != null)
            {
                foreach (var prop in Cmd.Like.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var method = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
                    var exp    = Expression.Call(left, method, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.In != null)
            {
                foreach (var prop in Cmd.In.Properties())
                {
                    var right = Expression.PropertyOrField(param, prop.Name);

                    Type coll    = typeof(List <>);
                    Type collOfT = coll.MakeGenericType(right.Type);

                    dynamic collection = Activator.CreateInstance(collOfT);

                    foreach (var item in prop.Value)
                    {
                        var converter = TypeDescriptor.GetConverter(right.Type);
                        if (converter != null)
                        {
                            dynamic ov = converter.ConvertFromString(item.ToString());
                            collection.Add(ov);
                        }
                    }
                    Expression left = Expression.Constant(collection, collOfT);

                    var method = left.Type.GetMethod("Contains", new Type[] { right.Type });
                    var exp    = Expression.Call(left, method, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.Range != null)
            {
                foreach (var prop in Cmd.Range.Properties())
                {
                    var left = Expression.PropertyOrField(param, prop.Name);

                    var val1   = Convert.ChangeType(prop.Value["min"].ToString(), left.Type);
                    var right1 = Expression.Constant(val1, left.Type);
                    var exp1   = Expression.GreaterThanOrEqual(left, right1);

                    var val2   = Convert.ChangeType(prop.Value["max"].ToString(), left.Type);
                    var right2 = Expression.Constant(val2, left.Type);
                    var exp2   = Expression.LessThanOrEqual(left, right2);

                    var exp = Expression.And(exp1, exp2);

                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.Gt != null)
            {
                foreach (var prop in Cmd.Gt.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var exp    = Expression.GreaterThan(left, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.GtEq != null)
            {
                foreach (var prop in Cmd.GtEq.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var exp    = Expression.GreaterThanOrEqual(left, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.Lt != null)
            {
                foreach (var prop in Cmd.Lt.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var exp    = Expression.LessThan(left, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            if (Cmd.LtEq != null)
            {
                foreach (var prop in Cmd.LtEq.Properties())
                {
                    var left   = Expression.PropertyOrField(param, prop.Name);
                    var val    = Convert.ChangeType(prop.Value.ToString(), left.Type);
                    var right  = Expression.Constant(val, left.Type);
                    var exp    = Expression.LessThanOrEqual(left, right);
                    var lambda = Expression.Lambda <Func <TEntity, bool> >(exp, param);
                    where = where.And(lambda);
                }
            }

            query = query.Where(where);
            return(query);
        }
예제 #2
0
        public static IQueryable <TEntity> AutoSort <TEntity>(this IQueryable <TEntity> query, PageCommand Cmd)
            where TEntity : class
        {
            var param = Expression.Parameter(typeof(TEntity), "x");

            if (Cmd.Sort == null)
            {
                return(query);
            }
            var propertys = Cmd.Sort.Properties().ToList();

            for (int i = 0; i < propertys.Count(); i++)
            {
                var prop = propertys[i];

                Expression conversion = Expression.Convert(Expression.PropertyOrField(param, prop.Name), typeof(object));
                var        lambda     = Expression.Lambda <Func <TEntity, object> >(conversion, param);

                var val = prop.Value.ToString();
                if (val == "asc")
                {
                    if (i == 0)
                    {
                        query = query.OrderBy(lambda);
                    }
                    else
                    {
                        query = ((IOrderedQueryable <TEntity>)query).ThenBy(lambda);
                    }
                }
                else if (val == "desc")
                {
                    if (i == 0)
                    {
                        query = query.OrderByDescending(lambda);
                    }
                    else
                    {
                        query = ((IOrderedQueryable <TEntity>)query).ThenByDescending(lambda);
                    }
                }
            }
            return(query);
        }
예제 #3
0
        public static PageResult <TResult> ToPage <TEntity, TResult>(this IQueryable <TEntity> query, PageCommand Cmd)
            where TEntity : class
        {
            query = query.AutoWhere(Cmd);
            query = query.AutoSort(Cmd);

            if (!(Cmd.PageIndex == 0 || Cmd.PageSize == 0))
            {
                query = query.Skip((Cmd.PageIndex - 1) * Cmd.PageSize).Take(Cmd.PageSize);
            }

            var sql = query.AutoSelect <TEntity, TResult>().ToSql();


            long total = query.Count();
            var  list  = query.AutoSelect <TEntity, TResult>().ToList();

            return(new PageResult <TResult>(list, total));
        }