jqgrid with asp.net webmethod and json working with sorting, paging, searching and LINQ — but needs dynamic operators

后端 未结 2 1264
囚心锁ツ
囚心锁ツ 2021-01-30 02:39

THIS WORKS! .. but still needs one more thing...

Okay, so this is both a \"comment\" and question. First, is the working example that may help

相关标签:
2条回答
  • 2021-01-30 03:10

    Give this article a peep. It is focused on using jqgrid in MVC but you can extract the relevant information.

    0 讨论(0)
  • 2021-01-30 03:21

    Consider this extension method, that converts a string into a MemberExpression:

    public static class StringExtensions
    {
        public static MemberExpression ToMemberExpression(this string source, ParameterExpression p)
        {
            if (p == null)
                throw new ArgumentNullException("p");
    
            string[] properties = source.Split('.');
    
            Expression expression = p;
            Type type = p.Type;
    
            foreach (var prop in properties)
            {
                var property = type.GetProperty(prop);
                if (property == null)
                    throw new ArgumentException("Invalid expression", "source");
    
                expression = Expression.MakeMemberAccess(expression, property);
                type = property.PropertyType;
            }
    
            return (MemberExpression)expression;
        }
    }
    

    The method below converts the strings that you have into an Lambda Expression, that you can use to filter a Linq query. It is a generic method, with T as the domain entity.

        public virtual Expression<Func<T, bool>> CreateExpression<T>(string searchField, string searchString, string searchOper)
        {
            Expression exp = null;
            var p = Expression.Parameter(typeof(T), "p");
    
            try
            {
                Expression propertyAccess = searchField.ToExpression(p);
    
                switch (searchOper)
                {
                    case "bw":
                        exp = Expression.Call(propertyAccess, typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }), Expression.Constant(searchString));
                        break;
                    case "cn":
                        exp = Expression.Call(propertyAccess, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(searchString));
                        break;
                    case "ew":
                        exp = Expression.Call(propertyAccess, typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) }), Expression.Constant(searchString));
                        break;
                    case "gt":
                        exp = Expression.GreaterThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                        break;
                    case "ge":
                        exp = Expression.GreaterThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                        break;
                    case "lt":
                        exp = Expression.LessThan(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                        break;
                    case "le":
                        exp = Expression.LessThanOrEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                        break;
                    case "eq":
                        exp = Expression.Equal(propertyAccess, Expression.Constant(searchString.ToType(propertyAccess.Type), propertyAccess.Type));
                        break;
                    case "ne":
                        exp = Expression.NotEqual(propertyAccess, Expression.Constant(searchString, propertyAccess.Type));
                        break;
                    default:
                        return null;
                }
    
                return (Expression<Func<T, bool>>)Expression.Lambda(exp, p);
            }
            catch
            {
                return null;
            }
        }
    

    So, you can use it like this:

    db.TBL_USERs.Where(CreateExpression<TBL_USER>("LAST_NAME", "Costa", "eq"));
    
    0 讨论(0)
提交回复
热议问题