zoukankan      html  css  js  c++  java
  • Linq动态查询

    public class ExpressionCall
        {
            List<Customer> customers = new List<Customer>() {
                new Customer() { CustomerID = "A001"},
                new Customer() { CustomerID = "A"},
                new Customer() { CustomerID = "B001" },
        };
            string[] starts = { "A", "C", "D" };
            public void SelectMore()
            {  //根据CustomerID首字母 包含A,C,D动态创建查询
                IQueryable<Customer> cus = customers.AsQueryable();
                ParameterExpression c = Expression.Parameter(typeof(Customer), "c");
                Expression condition = Expression.Constant(false);
                foreach (string s in starts)
                {
                    Expression con = Expression.Call(
                        Expression.Property(c, typeof(Customer).GetProperty("CustomerID")),
                        typeof(string).GetMethod("Equals", new Type[] { typeof(string) }),
                        Expression.Constant(s));
                    condition = Expression.Or(con, condition);
                }
                Expression<Func<Customer, bool>> end =
                    Expression.Lambda<Func<Customer, bool>>(condition, new ParameterExpression[] { c });
                var cu = cus.Where(end);
            }
        }
        public class Customer
        {
            public string CustomerID { get; set; }
        }
    View Code

    代码如上,参考:http://www.cnblogs.com/blusehuang/archive/2007/07/13/816970.html

    其中包含。首字母包含

                        typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
                        typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
  • 相关阅读:
    python网站开发准备ubuntu14.04安装mysql实现windows管理
    python 数据结构之二叉树
    python 数据结构之二分查找的递归和普通实现
    python 数据结构之归并排序
    python数据结构之希尔排序
    ctf study of jarvisoj reverse
    python数据结构之quick_sort
    堆与栈
    汇编整理
    js运算符
  • 原文地址:https://www.cnblogs.com/youngharvard/p/9009869.html
Copyright © 2011-2022 走看看