• 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) }),
  • 相关阅读:
    7. ZooKeeper的stat结构
    6. ZooKeeper访问控制列表
    5. 监视和ZooKeeper操作
    4. ZooKeeper 基本操作
    3.Apache ZooKeeper数据模型
    Eclipse安装Activiti Designer插件
    Javascript Canvas验证码
    Tomcat9配置SSL连接
    JAVA将异常的堆栈信息转成String
    SpringBoot2静态资料访问
  • 原文地址:https://www.cnblogs.com/youngharvard/p/9009869.html
Copyright © 2020-2023  润新知