Linqu dynamic query

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()
        {   // Create a query dynamically according to the first letter of CustomerID including 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

The code is as above, reference: http://www.cnblogs.com/blusehuang/archive/2007/07/13/816970.html

which contains. initials contain

                    typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
                    typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851476&siteId=291194637