Linqu动态查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linqu动态查询相关的知识,希望对你有一定的参考价值。
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; } }
代码如上,参考: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动态查询的主要内容,如果未能解决你的问题,请参考以下文章