码迷,mamicode.com
首页 > Windows程序 > 详细

C#基础 Lambda

时间:2020-06-29 13:11:07      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:ble   blog   and   ssi   code   any   using   query   lam   

原文:https://www.cnblogs.com/Lau7/p/5451985.html

扩展类

public static class ExpressionExt
{
    public static Expression<Func<T, bool>> And<T>(this?Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
    {
        return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expr1.Body, expr2.Body), expr1.Parameters);
    }
    public static Expression<Func<T, bool>> Or<T>(this?Expression<Func<T, bool>> expr1,Expression<Func<T, bool>> expr2)
    {
        return Expression.Lambda<Func<T, bool>>(Expression.OrElse(expr1.Body, expr2.Body), expr1.Parameters);
    }
}

使用示例

Expression<Func<SysUser, bool>> exp1 = s => s.UserName.Contains("1") && s.Age > 0;
Expression<Func<SysUser, bool>> exp2 =exp1.And( s => s.IsEnable == 1);
using (var context = new DbContext().ConnectionString(connString))
{
    var result1 = context.Select<SysUser>(exp1).QueryMany();
    var result2 = context.Select<SysUser>(exp2).QueryMany();
}

C#基础 Lambda

标签:ble   blog   and   ssi   code   any   using   query   lam   

原文地址:https://www.cnblogs.com/guxingy/p/13207203.html

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