c# 拼接lambda表达式
标签:turn stat res bsp dex 表达 compile 拼接 金刚
添加一个类:
public static class PredicateExtensions
{
public static Expressionbool>> True()
{
return f => true;
}
public static Expressionbool>> False()
{
return f => false;
}
public static Expressionbool>> Or(this Expressionbool>> expression1, Expressionbool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast());
return Expression.Lambdabool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
}
public static Expressionbool>> And(this Expressionbool>> expression1, Expressionbool>> expression2)
{
var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast());
return Expression.Lambdabool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
}
}
使用方法:
public ActionResult Index(string keyword)
{
string name = "变形金刚";
decimal price = 20000;
DateTime dt = Convert.ToDateTime("2019 - 09 - 09");
Expressionbool>> expression = t => true;
expression = expression.And(t => t.MName == name);
expression = expression.And(t => t.MPrice == price);
expression = expression.And(t => t.ShowTime == dt);
var ds = db.Movies.Where(expression.Compile()).AsQueryable().ToList();
return View(ds);
}
c# 拼接lambda表达式
标签:turn stat res bsp dex 表达 compile 拼接 金刚
原文地址:https://www.cnblogs.com/dcy521/p/11468768.html
评论