/// <summary> /// 获取字段存在一列表中的表达式 /// </summary> /// <param name="value">对象值列表</param> /// <returns>条件表达式</returns> public Expression In(IEnumerable <T> values) { string p; int i = 0; Expression exp = new Expression(); exp.SqlText.Append(" ").Append(Name).Append(" in ("); foreach (object value in values) { p = Expression.GetParamName(); if (i > 0) { exp.SqlText.Append(","); } exp.SqlText.Append("@").Append(p); exp.Parameters.Add(new Command.Parameter { Name = p, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, value) }); i++; } exp.SqlText.Append(" )"); return(exp); }
/// <summary> /// 获取字段不等于一个值的表达式 /// </summary> /// <param name="value">对象值</param> /// <returns>条件表达式</returns> public Expression NotEq(T value) { Expression exp = new Expression(); string p = Expression.GetParamName(); exp.SqlText.Append(string.Format(" {0}<>@{1} ", Name, p)); exp.Parameters.Add(new Command.Parameter { Name = p, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, value) }); return(exp); }
/// <summary> /// 获取字段和另一个表的字段非集条件表达式 /// </summary> /// <param name="field">并集字段</param> /// <param name="expression">相应表的条件查询表达式</param> /// <returns>条件表达式</returns> public Expression NotIn(FieldInfo <T> field, Expression expression) { Expression exp = new Expression(); string astable = "T" + Expression.GetParamName(); exp.SqlText.Append(" ").Append(Name).Append(" not in (select ").Append(astable).Append(".").Append(field.Name).Append(" from ").Append(field.Table).Append(" ").Append(astable); if (expression != null && expression.SqlText.Length > 0) { exp.SqlText.Append(" where ").Append(expression.SqlText.ToString()); exp.Parameters.AddRange(expression.Parameters); } exp.SqlText.Append(")"); return(exp); }
/// <summary> /// 获取这段不存在指定区间的表达式 /// </summary> /// <param name="fromvalue">开始值</param> /// <param name="tovalue">结束值</param> /// <returns>条件表达式</returns> public Expression NotBetween(T fromvalue, T tovalue) { string p, p1; Expression exp = new Expression(); p = Expression.GetParamName(); p1 = Expression.GetParamName(); exp.SqlText.Append(string.Format(" {0} not Between @{1} and @{2}", Name, p, p1)); exp.Parameters.Add(new Command.Parameter { Name = p, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, fromvalue) }); exp.Parameters.Add(new Command.Parameter { Name = p1, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, tovalue) }); return(exp); }
/// <summary> /// 返回字段匹配某一个值的表达式 /// </summary> /// <param name="value">值</param> /// <returns>条件表达式</returns> public Expression Like(object value) { if (value != null && value is System.Collections.IEnumerable && value.GetType() != typeof(string)) { return(LikeMany((System.Collections.IEnumerable)value)); } Expression exp = new Expression(); string pn = Expression.GetParamName(); exp.SqlText.Append( string.Format("{0} like @{1}", Name, pn) ); exp.Parameters.Add(new Command.Parameter { Name = pn, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, value) }); return(exp); }
/// <summary> /// 返回字段匹配相应列表值的表达式,此方法会在值前后添加通配符% /// </summary> /// <param name="value">值</param> /// <returns>条件表达式</returns> private Expression MatchMany(System.Collections.IEnumerable value) { Expression exp = new Expression(); int i = 0; string pn; exp.SqlText.Append("("); foreach (object item in value) { pn = Expression.GetParamName(); if (i > 0) { exp.SqlText.Append(" or "); } exp.SqlText.Append(Name).Append(" like @").Append(pn); exp.Parameters.Add(new Command.Parameter { Name = pn, Value = "%" + item + "%" }); i++; } exp.SqlText.Append(")"); return(exp); }
/// <summary> /// 返回字段匹配相应列表值的表达式 /// </summary> /// <param name="value">值</param> /// <returns>条件表达式</returns> private Expression LikeMany(System.Collections.IEnumerable value) { Expression exp = new Expression(); int i = 0; string pn; exp.SqlText.Append("("); foreach (object item in value) { pn = Expression.GetParamName(); if (i > 0) { exp.SqlText.Append(" or "); } exp.SqlText.Append(Name).Append(" like @").Append(pn); exp.Parameters.Add(new Command.Parameter { Name = pn, Value = Mappings.PropertyCastAttribute.CastValue(Table, Name, item) }); i++; } exp.SqlText.Append(")"); return(exp); }