///// <summary> ///// 使用 OR 逻辑符号拼接表达式树 ///// </summary> //public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) //{ // var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); // return Expression.Lambda<Func<T, bool>>(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters); //} ///// <summary> ///// 使用 AND 逻辑符号拼接表达式树 ///// </summary> //public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) //{ // var invokedExpr = Expression.Invoke(expr1, expr2.Parameters.Cast<Expression>()); // return Expression.Lambda<Func<T, bool>>(Expression.And(expr2.Body, invokedExpr), expr2.Parameters); //} private static Expression <T> Combine <T>(this Expression <T> first, Expression <T> second, Func <Expression, Expression, Expression> merge) { MyExpressionVisitor visitor = new MyExpressionVisitor(first.Parameters[0]); Expression bodyone = visitor.Visit(first.Body); Expression bodytwo = visitor.Visit(second.Body); return(Expression.Lambda <T>(merge(bodyone, bodytwo), first.Parameters[0])); }
public UserEntity GetOneByPredicate(Expression <Func <UserEntity, bool> > f) { var visitor = new MyExpressionVisitor <UserEntity, DalUser>(Expression.Parameter(typeof(DalUser), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <DalUser, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); return(userRepository.GetOneByPredicate(exp2).GetBllEntity()); }
public static Expression <Func <B> > BindInput(Expression <Func <A, B> > expression, A input) { var parameter = expression.Parameters.Single(); var visitor = new MyExpressionVisitor(parameter, input); return(Expression.Lambda <Func <B> >(visitor.Visit(expression.Body))); }
public IEnumerable <RoleEntity> GetAllByPredicate(Expression <Func <RoleEntity, bool> > f) { var visitor = new MyExpressionVisitor <RoleEntity, DalRole>(Expression.Parameter(typeof(DalRole), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <DalRole, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); return(roleRepository.GetAllByPredicate(exp2).Select(r => r.GetBllEntity())); }
public IEnumerable <UserEntity> GetAllByPredicate(Expression <Func <UserEntity, bool> > f) { var visitor = new MyExpressionVisitor <UserEntity, DalUser>(Expression.Parameter(typeof(DalUser), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <DalUser, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); //ToList() return(userRepository.GetAllByPredicate(exp2).Select(user => user.GetBllEntity()).ToList()); }
public IEnumerable <DalMission> GetAllByPredicate(Expression <Func <DalMission, bool> > f) { var visitor = new MyExpressionVisitor <DalMission, Mission>(Expression.Parameter(typeof(Mission), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <Mission, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); var x = context.Set <Mission>().Where(exp2).ToList(); return(x.Select(mis => mis.GetDalEntity())); }
public IEnumerable <DalUser> GetAllByPredicate(Expression <Func <DalUser, bool> > f) { var visitor = new MyExpressionVisitor <DalUser, User>(Expression.Parameter(typeof(User), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <User, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); var x = context.Set <User>().Include(user => user.Roles).Where(exp2).ToList(); return(x.Select(user => user.GetDalEntity())); }
public IEnumerable <DalTask> GetAllByPredicate(Expression <Func <DalTask, bool> > f) { var visitor = new MyExpressionVisitor <DalTask, Task>(Expression.Parameter(typeof(Task), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <Task, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); var tasks = context.Set <Task>().Where(exp2).ToList(); return(tasks.Select(task => task.GetDalEntity())); }
public IEnumerable <MissionEntity> GetAllByPredicate(Expression <Func <MissionEntity, bool> > f) { var visitor = new MyExpressionVisitor <MissionEntity, DalMission>(Expression.Parameter(typeof(DalMission), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <DalMission, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); var mission = missionRepository.GetAllByPredicate(exp2).ToList(); return(mission.Select(m => m.GetBllEntity())); }
public IEnumerable <DalRole> GetAllByPredicate(Expression <Func <DalRole, bool> > f) { var visitor = new MyExpressionVisitor <DalRole, Role>(Expression.Parameter(typeof(Role), f.Parameters[0].Name)); var exp2 = Expression.Lambda <Func <Role, bool> >(visitor.Visit(f.Body), visitor.NewParameterExp); return(context.Set <Role>() .Where(exp2) .Select(r => r.GetDalEntity())); }
public static T FirstOrDefault <T>(Expression <Func <T, bool> > expression) where T : class { var visit = new MyExpressionVisitor(); visit.Visit(expression); var sqlWhere = visit.WhereMarkUp <T>(); var tableAttr = GetTableAttribute <T>(); var sql = SqlReplaceWhiteSpace($"Select Top 1 * from {tableAttr.TableName}{ sqlWhere} ;"); IList <T> result = GetFillDataSet <T>(sql); return(result.FirstOrDefault()); }
public static bool Update <T>(Expression <Func <T, bool> > expression, dynamic updateObj) where T : class { var visit = new MyExpressionVisitor(); visit.Visit(expression); var sqlWhere = visit.WhereMarkUp <T>(); var tableAttr = GetTableAttribute <T>(); var setParam = GetSetParam(updateObj); var sql = SqlReplaceWhiteSpace($"Update {tableAttr.TableName} Set {setParam}{ sqlWhere} ;"); var res = DBHelper.ExecuteNonQuery(sql); return(res > 0 ? true : false); }
public static int Count <T>(Expression <Func <T, bool> > expression) { var visit = new MyExpressionVisitor(); visit.Visit(expression); var sqlWhere = visit.WhereMarkUp <T>(); var tableAttr = GetTableAttribute <T>(); var sql = SqlReplaceWhiteSpace($"Select Count(*) from {tableAttr.TableName}{ sqlWhere} ;"); var result = DBHelper.ExecuteScalar(sql); var count = Convert.ToInt32(result); return(count); }
static void Main(string[] args) { Expression <Func <DateTime, double> > abc = v => 1.0d * v.Ticks / (v.Month + v.Minute); MyExpressionVisitor mev = new MyExpressionVisitor(DateTime.Now); var ret = mev.Visit(abc); }