using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Linq.Expressions;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace HH.WCS.Mobox3.WeiLi.wms
|
{
|
internal static class ExpressionHelper {
|
public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right) {
|
var invokedExpr = Expression.Invoke(right, left.Parameters);
|
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(left.Body, invokedExpr), left.Parameters);
|
}
|
public class Person {
|
public string Name { get; set; }
|
public int Age { get; set; }
|
}
|
public static void test() {
|
var list=new List<Person>();
|
list.Add(new Person {Age=1, Name="111" });
|
list.Add(new Person { Age = 1, Name = "222" });
|
list.Add(new Person { Age = 1, Name = "333" });
|
list.Add(new Person { Age = 2, Name = "111" });
|
list.Add(new Person { Age = 2, Name = "222" });
|
list.Add(new Person { Age = 2, Name = "333" });
|
list.Add(new Person { Age = 3, Name = "111" });
|
list.Add(new Person { Age = 3, Name = "222" });
|
list.Add(new Person { Age = 3, Name = "333" });
|
|
|
Expression<Func<Person, bool>> expression = x => x.Age >= 2;
|
|
expression = expression.AndAlso(x => x.Name =="111");
|
|
var data=list.Where(expression.Compile()).ToList();
|
}
|
}
|
}
|