lambda Expression的使用方法

Expression<Func<your class, bool>> whereExp = f => true;//类似1=1,初始化条件

            if (!string.IsNullOrWhiteSpace(userID))
            {
                whereExp = whereExp.And(f => f.UserID.Equals(userID));
            }

            if (!string.IsNullOrWhiteSpace(userName))
            {
                whereExp = whereExp.And(f => f.userName.Equals(userName));
            }

这样一个Expression就写好了,可以放在ef中使用,也可以放在nhibernate的条件位置,例如:

YourService.QueryListByLinq(whereExp: whereExp).FirstOrDefault();

这句话就是按照条件查询一条数据库信息的nhibernate的写法

猜你喜欢

转载自www.cnblogs.com/llcdbk/p/9075218.html
今日推荐