private static AbstractSqlCondition ParseBinaryExpression(AbstractSqlCommandWithWhere sourceCommand, BinaryExpression expr) { switch (expr.NodeType) { case ExpressionType.Equal: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.Equal)); case ExpressionType.NotEqual: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.NotEqual)); case ExpressionType.GreaterThan: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.GreaterThan)); case ExpressionType.LessThan: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.LessThan)); case ExpressionType.GreaterThanOrEqual: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.GreaterThanOrEqual)); case ExpressionType.LessThanOrEqual: return(ParseBinaryExpression(sourceCommand, expr, SqlOperator.LessThanOrEqual)); case ExpressionType.And: case ExpressionType.AndAlso: return(SqlLinqCondition.ParseCondition(sourceCommand, expr.Left) & SqlLinqCondition.ParseCondition(sourceCommand, expr.Right)); case ExpressionType.Or: case ExpressionType.OrElse: return(SqlLinqCondition.ParseCondition(sourceCommand, expr.Left) | SqlLinqCondition.ParseCondition(sourceCommand, expr.Right)); default: throw new LinqNotSupportedException("Not supported this linq operation!"); } }
/// <summary> /// 设置指定查询的语句并返回当前语句 /// </summary> /// <typeparam name="T">实体类类型</typeparam> /// <param name="cmd">更新语句</param> /// <param name="expr">Linq表达式</param> /// <exception cref="LinqNotSupportedException">Linq操作不支持</exception> /// <returns>当前语句</returns> /// <example> /// <code lang="C#"> /// <![CDATA[ /// public class UserRepository : DatabaseTable<User> /// { /// //other necessary code /// /// public Boolean UpdateEntity(User user) /// { /// return this.Update() /// .Set<User>(c => c.UserName, user.UserName) /// .Where<User>(c => c.UserID == user.UserID) /// .Result() > 0; /// /// //UPDATE tbl_Users SET UserName = @UserName WHERE UserID = @UserID /// //@UserName = user.UserName /// //@UserID = user.UserID /// } /// } /// ]]> /// </code> /// </example> public static UpdateCommand Where <T>(this UpdateCommand cmd, Expression <Func <T, Boolean> > expr) { return(cmd.Where(SqlLinqCondition.Create <T>(cmd, expr))); }
/// <summary> /// 设置指定查询的语句并返回当前语句 /// </summary> /// <typeparam name="T">实体类类型</typeparam> /// <param name="cmd">选择语句</param> /// <param name="expr">Linq表达式</param> /// <exception cref="LinqNotSupportedException">Linq操作不支持</exception> /// <returns>当前语句</returns> public static SelectCommand Having <T>(this SelectCommand cmd, Expression <Func <T, Boolean> > expr) { return(cmd.Having(SqlLinqCondition.Create <T>(cmd, expr))); }
/// <summary> /// 创建新的Sql条件语句 /// </summary> /// <typeparam name="T">实体类类型</typeparam> /// <param name="sourceCommand">来源语句</param> /// <param name="expr">Linq表达式</param> /// <exception cref="LinqNotSupportedException">Linq操作不支持</exception> /// <exception cref="NullAttributeException">没有设置特性</exception> /// <returns>Sql条件语句</returns> public static AbstractSqlCondition Create <T>(AbstractSqlCommandWithWhere sourceCommand, Expression <Func <T, Boolean> > expr) { return(SqlLinqCondition.ParseCondition(sourceCommand, expr.Body)); }