Skip to content

Commit acc1754

Browse files
2881028810
authored andcommitted
修复 ISelect2 以上 WhereIf 条件作用反了 bug
1 parent 8d266a5 commit acc1754

File tree

12 files changed

+43
-29
lines changed

12 files changed

+43
-29
lines changed

FreeSql.Tests/UnitTest1.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,33 @@ public void Test1() {
143143
).ToList();
144144

145145

146+
var groupbysql = g.mysql.Select<TestInfo>().From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
147+
.Where(a => a.Id == 1)
148+
.WhereIf(false, a => a.Id == 2)
149+
)
150+
.WhereIf(true, (a, b, c) => a.Id == 3)
151+
.GroupBy((a, b, c) => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 })
152+
.Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0)
153+
.Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100)
154+
.OrderBy(a => a.Key.tt2)
155+
.OrderByDescending(a => a.Count()).ToSql(a => new { a.Key.mod4, a.Key.tt2 });
156+
157+
var groupbysql2 = g.mysql.Select<TestInfo>().From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
158+
.Where(a => a.Id == 1)
159+
.WhereIf(true, a => a.Id == 2)
160+
)
161+
.WhereIf(false, (a, b, c) => a.Id == 3)
162+
.GroupBy((a, b, c) => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 })
163+
.Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0)
164+
.Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100)
165+
.OrderBy(a => a.Key.tt2)
166+
.OrderByDescending(a => a.Count()).ToSql(a => a.Key.mod4);
167+
146168
var groupby = g.mysql.Select<TestInfo>().From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
147169
.Where(a => a.Id == 1)
170+
.WhereIf(true, a => a.Id == 2)
148171
)
172+
.WhereIf(true, (a,b,c) => a.Id == 3)
149173
.GroupBy((a, b, c) => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 })
150174
.Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0)
151175
.Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100)

FreeSql/FreeSql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.5.1.2</Version>
5+
<Version>0.5.1.3</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>

FreeSql/Internal/CommonProvider/SelectProvider/Select10Provider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ISelect<T1, T2, T3, T4, T5, T6,
147147
}
148148

149149
ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> ISelect<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, bool>> exp) {
150-
if (condition) return this.Where(null);
151-
if (exp == null) return this.Where(null);
150+
if (condition == false || exp == null) return this.Where(null);
152151
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
153152
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
154153
}

FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ public ISelect<T1> Where<T2, T3, T4, T5>(Expression<Func<T1, T2, T3, T4, T5, boo
221221
public ISelect<T1> WhereDynamic(object dywhere) => this.Where(_commonUtils.WhereObject(_tables.First().Table, $"{_tables.First().Alias}.", dywhere));
222222

223223
public ISelect<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp) {
224-
if (condition == false) return this;
225-
if (exp == null) return this;
224+
if (condition == false || exp == null) return this;
226225
_tables[0].Parameter = exp.Parameters[0];
227226
return this.InternalWhere(exp?.Body);
228227
}

FreeSql/Internal/CommonProvider/SelectProvider/Select2Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,9 @@ ISelect<T1, T2> ISelect<T1, T2>.Where(Expression<Func<T1, T2, bool>> exp) {
131131
}
132132

133133
ISelect<T1, T2> ISelect<T1, T2>.WhereIf(bool condition, Expression<Func<T1, T2, bool>> exp) {
134-
if (condition) return this.Where(null);
135-
if (exp == null) return this.Where(null);
134+
if (condition == false || exp == null) return this;
136135
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
137-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
136+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
138137
}
139138

140139
bool ISelect<T1, T2>.Any(Expression<Func<T1, T2, bool>> exp) {

FreeSql/Internal/CommonProvider/SelectProvider/Select3Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ ISelect<T1, T2, T3> ISelect<T1, T2, T3>.Where(Expression<Func<T1, T2, T3, bool>>
133133
}
134134

135135
ISelect<T1, T2, T3> ISelect<T1, T2, T3>.WhereIf(bool condition, Expression<Func<T1, T2, T3, bool>> exp) {
136-
if (condition) return this.Where(null);
137-
if (exp == null) return this.Where(null);
136+
if (condition == false || exp == null) return this;
138137
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
139-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
138+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
140139
}
141140

142141
bool ISelect<T1, T2, T3>.Any(Expression<Func<T1, T2, T3, bool>> exp) {

FreeSql/Internal/CommonProvider/SelectProvider/Select4Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ ISelect<T1, T2, T3, T4> ISelect<T1, T2, T3, T4>.Where(Expression<Func<T1, T2, T3
135135
}
136136

137137
ISelect<T1, T2, T3, T4> ISelect<T1, T2, T3, T4>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, bool>> exp) {
138-
if (condition) return this.Where(null);
139-
if (exp == null) return this.Where(null);
138+
if (condition == false || exp == null) return this;
140139
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
141-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
140+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
142141
}
143142

144143
bool ISelect<T1, T2, T3, T4>.Any(Expression<Func<T1, T2, T3, T4, bool>> exp) {

FreeSql/Internal/CommonProvider/SelectProvider/Select5Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,9 @@ ISelect<T1, T2, T3, T4, T5> ISelect<T1, T2, T3, T4, T5>.Where(Expression<Func<T1
137137
}
138138

139139
ISelect<T1, T2, T3, T4, T5> ISelect<T1, T2, T3, T4, T5>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, bool>> exp) {
140-
if (condition) return this.Where(null);
141-
if (exp == null) return this.Where(null);
140+
if (condition == false || exp == null) return this;
142141
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
143-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
142+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
144143
}
145144

146145
bool ISelect<T1, T2, T3, T4, T5>.Any(Expression<Func<T1, T2, T3, T4, T5, bool>> exp) {

FreeSql/Internal/CommonProvider/SelectProvider/Select6Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ ISelect<T1, T2, T3, T4, T5, T6> ISelect<T1, T2, T3, T4, T5, T6>.Where(Expression
139139
}
140140

141141
ISelect<T1, T2, T3, T4, T5, T6> ISelect<T1, T2, T3, T4, T5, T6>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp) {
142-
if (condition) return this.Where(null);
143-
if (exp == null) return this.Where(null);
142+
if (condition == false || exp == null) return this;
144143
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
145-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
144+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
146145
}
147146

148147
bool ISelect<T1, T2, T3, T4, T5, T6>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, bool>> exp) {

FreeSql/Internal/CommonProvider/SelectProvider/Select7Provider.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ ISelect<T1, T2, T3, T4, T5, T6, T7> ISelect<T1, T2, T3, T4, T5, T6, T7>.Where(Ex
141141
}
142142

143143
ISelect<T1, T2, T3, T4, T5, T6, T7> ISelect<T1, T2, T3, T4, T5, T6, T7>.WhereIf(bool condition, Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp) {
144-
if (condition) return this.Where(null);
145-
if (exp == null) return this.Where(null);
144+
if (condition == false || exp == null) return this;
146145
for (var a = 0; a < exp.Parameters.Count; a++) _tables[a].Parameter = exp.Parameters[a];
147-
return condition ? this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null)) : this;
146+
return this.Where(_commonExpression.ExpressionWhereLambda(_tables, exp?.Body, null));
148147
}
149148

150149
bool ISelect<T1, T2, T3, T4, T5, T6, T7>.Any(Expression<Func<T1, T2, T3, T4, T5, T6, T7, bool>> exp) {

0 commit comments

Comments
 (0)