码迷,mamicode.com
首页 > 其他好文 > 详细

Linqu动态查询

时间:2018-05-08 19:30:17      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:express   ssi   .com   als   arch   property   constant   fun   custom   

技术分享图片
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) }),

 

Linqu动态查询

标签:express   ssi   .com   als   arch   property   constant   fun   custom   

原文地址:https://www.cnblogs.com/youngharvard/p/9009869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!