none
c#有没有类似js语言的eval方法,可以把一段字符串转成一段c#语句代码,并执行这段动态生成的c#语句代码方法?

    问题

  • public List<Student> Sort(string SortProperty, List<Student> SortList) { List<Student> sortReach = null; string dynamicExecuteCode= ""+ "sortReach = (from student in SortList " + " orderby student."+(SortProperty) +" ascending "+ " select student).toList()";

    //Eval(dynamicExecuteCode); return sortReach; }


    如何将上面的代码中的string变量dynamicExecuteCode所装的内容转成一段代码执行,dynamicExecuteCode里的字符串的内容就是一个linq语句。根据SortProperty这个参数动态决定对集合的哪个属性进行排序,上面那个程序代码只是表述问题,而我排序的属性数量有两个和三个。

    如何实现?c#有没有类似js语言中的Eval方法?

    http://blog.csdn.net/ghostbear/article/details/7764510 找到了一个他人实现的Eval的c#l版本,但是我测试发现似乎不支持赋值语句。  例如"str=\"动态赋值内容\";"













    2017年2月2日 8:22

答案

  • 如果要对2个属性或三个属性进行排序了?(不考虑四个),排序属性的类型只有数值类型和字符串类型

    哈! 其實你仔細想想,一般來說, db.Students.OrderBy(e => e.Id) 就好像我目前的code

    那麼如果要對第二個屬性排序,一般來說會這樣做...

    var result = db.Students.OrderBy(e => e.Id).ThenBy(e => e.Name);

    而這樣子也可以拆成這樣:

    var tempSort = db.Students.OrderBy(e => e.Id);

    var result = tempSort.ThenBy(e => e.Name);

    所以你就可以把我的整個程式要複製一次,只是欄位跟 方法名稱換掉,然後再 CreatQuery<T>(expression) 一次。

    以下程式碼,你再試試看,三次 四次 也是一樣 但是通常這種 排序在排序 三層就差不多了。

        class Program
        {
            static void Main(string[] args)
            {
                List<Student> students = GetStudents();
    
                var sq = students.AsQueryable();
    
                // 第一個條件
                ParameterExpression pe1 = Expression.Parameter(sq.ElementType, "");
                MemberExpression me1 = Expression.Property(pe1, "ClassNum"); // 欄位名稱
                LambdaExpression le1 = Expression.Lambda(me1, pe1);
    
                Expression expression1 = Expression.Call(typeof(Queryable),
                    "OrderBy", // "Order" or "OrderByDescending"
                    new Type[] { sq.ElementType, me1.Type },
                    sq.Expression,
                    Expression.Quote(le1));
    
                var list = sq.Provider.CreateQuery<Student>(expression1);
    
                // 第二個條件
                ParameterExpression pe2 = Expression.Parameter(list.ElementType, "");
                MemberExpression me2 = Expression.Property(pe2, "Name"); // 欄位名稱
                LambdaExpression le2 = Expression.Lambda(me2, pe2);
    
                Expression expression2 = Expression.Call(typeof(Queryable),
                    "ThenBy", // "ThenBy" or "ThenByDescending "
                    new Type[] { list.ElementType, me2.Type },
                    list.Expression,
                    Expression.Quote(le2));
    
                students = list.Provider.CreateQuery<Student>(expression2).ToList();
    
                foreach (var s in students)
                {
                    Console.WriteLine(s.Id + "\t" + s.Name + "\t" + s.ClassNum);
                }
            }
    
            private static List<Student> GetStudents()
            {
                var list = new List<Student>();
    
                list.Add(new Student() { Id = 1, Name = "Peter", ClassNum = 305 });
                list.Add(new Student() { Id = 2, Name = "Amy", ClassNum = 304 });
                list.Add(new Student() { Id = 3, Name = "Ann", ClassNum = 303 });
                list.Add(new Student() { Id = 4, Name = "Apple", ClassNum = 302 });
                list.Add(new Student() { Id = 5, Name = "Helen", ClassNum = 301 });
                list.Add(new Student() { Id = 6, Name = "Bebe", ClassNum = 305 });
                list.Add(new Student() { Id = 7, Name = "Shin", ClassNum = 304 });
                list.Add(new Student() { Id = 8, Name = "Jennifer", ClassNum = 303 });
    
                return list;
            }
        }

    2017年2月4日 10:48

全部回复

  • class Program { static void Main(string[] args) { List<Student> students = GetStudents(); var sq = students.AsQueryable(); ParameterExpression pe = Expression.Parameter(sq.ElementType, ""); MemberExpression me = Expression.Property(pe, "ClassNum"); // 欄位名稱 LambdaExpression le = Expression.Lambda(me, pe); Expression expression = Expression.Call(typeof(Queryable), "OrderBy", // "Order" or "OrderByDescending" new Type[] { sq.ElementType, me.Type }, sq.Expression, Expression.Quote(le)); students = sq.Provider.CreateQuery<Student>(expression).ToList(); foreach (var s in students) { Console.WriteLine(s.Id + "\t" + s.Name + "\t" + s.ClassNum); } } private static List<Student> GetStudents() { var list = new List<Student>(); list.Add(new Student() { Id = 1, Name = "Peter", ClassNum = 305 }); list.Add(new Student() { Id = 2, Name = "Amy", ClassNum = 304 }); list.Add(new Student() { Id = 3, Name = "Ann", ClassNum = 303 }); list.Add(new Student() { Id = 4, Name = "Apple", ClassNum = 302 }); list.Add(new Student() { Id = 5, Name = "Helen", ClassNum = 301 }); list.Add(new Student() { Id = 6, Name = "Bebe", ClassNum = 305 }); list.Add(new Student() { Id = 7, Name = "Shin", ClassNum = 304 }); list.Add(new Student() { Id = 8, Name = "Jennifer", ClassNum = 303 }); return list; }

    }

    我只想到這樣的方法 你參考
    2017年2月3日 3:59
  • class Program { static void Main(string[] args) { List<Student> students = GetStudents(); var sq = students.AsQueryable(); ParameterExpression pe = Expression.Parameter(sq.ElementType, ""); MemberExpression me = Expression.Property(pe, "ClassNum"); // 欄位名稱 LambdaExpression le = Expression.Lambda(me, pe); Expression expression = Expression.Call(typeof(Queryable), "OrderBy", // "Order" or "OrderByDescending" new Type[] { sq.ElementType, me.Type }, sq.Expression, Expression.Quote(le)); students = sq.Provider.CreateQuery<Student>(expression).ToList(); foreach (var s in students) { Console.WriteLine(s.Id + "\t" + s.Name + "\t" + s.ClassNum); } } private static List<Student> GetStudents() { var list = new List<Student>(); list.Add(new Student() { Id = 1, Name = "Peter", ClassNum = 305 }); list.Add(new Student() { Id = 2, Name = "Amy", ClassNum = 304 }); list.Add(new Student() { Id = 3, Name = "Ann", ClassNum = 303 }); list.Add(new Student() { Id = 4, Name = "Apple", ClassNum = 302 }); list.Add(new Student() { Id = 5, Name = "Helen", ClassNum = 301 }); list.Add(new Student() { Id = 6, Name = "Bebe", ClassNum = 305 }); list.Add(new Student() { Id = 7, Name = "Shin", ClassNum = 304 }); list.Add(new Student() { Id = 8, Name = "Jennifer", ClassNum = 303 }); return list; }

    }

    我只想到這樣的方法 你參考
    如果要对2个属性或三个属性进行排序了?(不考虑四个),排序属性的类型只有数值类型和字符串类型
    2017年2月4日 9:53
  • 如果要对2个属性或三个属性进行排序了?(不考虑四个),排序属性的类型只有数值类型和字符串类型

    哈! 其實你仔細想想,一般來說, db.Students.OrderBy(e => e.Id) 就好像我目前的code

    那麼如果要對第二個屬性排序,一般來說會這樣做...

    var result = db.Students.OrderBy(e => e.Id).ThenBy(e => e.Name);

    而這樣子也可以拆成這樣:

    var tempSort = db.Students.OrderBy(e => e.Id);

    var result = tempSort.ThenBy(e => e.Name);

    所以你就可以把我的整個程式要複製一次,只是欄位跟 方法名稱換掉,然後再 CreatQuery<T>(expression) 一次。

    以下程式碼,你再試試看,三次 四次 也是一樣 但是通常這種 排序在排序 三層就差不多了。

        class Program
        {
            static void Main(string[] args)
            {
                List<Student> students = GetStudents();
    
                var sq = students.AsQueryable();
    
                // 第一個條件
                ParameterExpression pe1 = Expression.Parameter(sq.ElementType, "");
                MemberExpression me1 = Expression.Property(pe1, "ClassNum"); // 欄位名稱
                LambdaExpression le1 = Expression.Lambda(me1, pe1);
    
                Expression expression1 = Expression.Call(typeof(Queryable),
                    "OrderBy", // "Order" or "OrderByDescending"
                    new Type[] { sq.ElementType, me1.Type },
                    sq.Expression,
                    Expression.Quote(le1));
    
                var list = sq.Provider.CreateQuery<Student>(expression1);
    
                // 第二個條件
                ParameterExpression pe2 = Expression.Parameter(list.ElementType, "");
                MemberExpression me2 = Expression.Property(pe2, "Name"); // 欄位名稱
                LambdaExpression le2 = Expression.Lambda(me2, pe2);
    
                Expression expression2 = Expression.Call(typeof(Queryable),
                    "ThenBy", // "ThenBy" or "ThenByDescending "
                    new Type[] { list.ElementType, me2.Type },
                    list.Expression,
                    Expression.Quote(le2));
    
                students = list.Provider.CreateQuery<Student>(expression2).ToList();
    
                foreach (var s in students)
                {
                    Console.WriteLine(s.Id + "\t" + s.Name + "\t" + s.ClassNum);
                }
            }
    
            private static List<Student> GetStudents()
            {
                var list = new List<Student>();
    
                list.Add(new Student() { Id = 1, Name = "Peter", ClassNum = 305 });
                list.Add(new Student() { Id = 2, Name = "Amy", ClassNum = 304 });
                list.Add(new Student() { Id = 3, Name = "Ann", ClassNum = 303 });
                list.Add(new Student() { Id = 4, Name = "Apple", ClassNum = 302 });
                list.Add(new Student() { Id = 5, Name = "Helen", ClassNum = 301 });
                list.Add(new Student() { Id = 6, Name = "Bebe", ClassNum = 305 });
                list.Add(new Student() { Id = 7, Name = "Shin", ClassNum = 304 });
                list.Add(new Student() { Id = 8, Name = "Jennifer", ClassNum = 303 });
    
                return list;
            }
        }

    2017年2月4日 10:48