From 4ea657d12e0282cdd61c83f6188ceb768595298f Mon Sep 17 00:00:00 2001
From: 2881099 <2881099@qq.com>
Date: Sun, 20 Apr 2025 16:45:29 +0800
Subject: [PATCH] =?UTF-8?q?-=20ZeroDbContext=20=E8=A1=A5=E5=85=85=20WhereI?=
=?UTF-8?q?f=EF=BC=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ZeroDbContext.SelectImpl.cs | 47 +++++++++++++++----
1 file changed, 38 insertions(+), 9 deletions(-)
diff --git a/Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.SelectImpl.cs b/Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.SelectImpl.cs
index c797062e9..5aa40e76c 100644
--- a/Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.SelectImpl.cs
+++ b/Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.SelectImpl.cs
@@ -589,26 +589,37 @@ namespace FreeSql.Extensions.ZeroEntity
/// WHERE [field] IN (...)
///
public SelectImpl WhereIn(string field, object value) => Where(field, "in", value);
+ public SelectImpl WhereInIf(bool condition, string field, object value) => condition ? Where(field, "in", value) : this;
///
/// WHERE [field] NOT IN (...)
///
public SelectImpl WhereNotIn(string field, object value) => Where(field, "!in", value);
+ public SelectImpl WhereNotInIf(bool condition, string field, object value) => condition ? Where(field, "!in", value) : this;
///
- /// Where(new { Year = 2017, CategoryId = 198, IsPublished = true })
- /// WHERE [Year] = 2017 AND [CategoryId] = 198 AND [IsPublished] = 1
+ /// 匿名对象:Where(new { Year = 2017, CategoryId = 198, IsPublished = true })
+ /// 字典对象:Where(new Dictionary<string, object> { ["Year"] = 2017, ["CategoryId"] = 198, ["IsPublished"] = true })
+ /// WHERE [Year] = 2017 AND [CategoryId] = 198 AND [IsPublished] = 1
///
- public SelectImpl Where(object multipleFields)
+ public SelectImpl WhereDynamic(object dywhere)
{
- if (multipleFields == null) return this;
- foreach (var prop in multipleFields.GetType().GetProperties())
- WhereDynamicFilter(new DynamicFilterInfo { Field = prop.Name, Operator = DynamicFilterOperator.Eq, Value = prop.GetValue(multipleFields, null) });
+ if (dywhere == null) return this;
+ if (dywhere is Dictionary dict)
+ {
+ foreach(var kv in dict)
+ WhereDynamicFilter(new DynamicFilterInfo { Field = kv.Key, Operator = DynamicFilterOperator.Eq, Value = kv.Value });
+ return this;
+ }
+ foreach (var prop in dywhere.GetType().GetProperties())
+ WhereDynamicFilter(new DynamicFilterInfo { Field = prop.Name, Operator = DynamicFilterOperator.Eq, Value = prop.GetValue(dywhere, null) });
return this;
}
+ public SelectImpl WhereDynamicIf(bool condition, object dywhere) => condition ? WhereDynamic(dywhere) : this;
///
/// WHERE [field] = ..
/// 更全请看重载 Where(string field, string @operator, object value)
///
public SelectImpl Where(string field, object value) => WhereDynamicFilter(new DynamicFilterInfo { Field = field, Operator = DynamicFilterOperator.Eq, Value = value });
+ public SelectImpl WhereIf(bool condition, string field, object value) => condition ? Where(field, value) : this;
///
/// WHERE [field]
/// !=、>、>=、<、<=、like、!like、in、!in
@@ -652,8 +663,18 @@ namespace FreeSql.Extensions.ZeroEntity
return WhereDynamicFilter(new DynamicFilterInfo { Field = field, Operator = DynamicFilterOperator.NotAny, Value = value });
}
throw new Exception($"未实现 {@operator}");
- }
- public SelectImpl WhereColumns(string field1, string @operator, string field2)
+ }
+ public SelectImpl WhereIf(bool condition, string field, string @operator, object value) => condition ? Where(field, @operator, value) : this;
+
+ ///
+ /// WHERE 字段与字段
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public SelectImpl WhereColumns(string field1, string @operator, string field2)
{
var field1Result = ParseField(null, null, field1);
if (field1Result == null) throw new Exception($"未匹配字段名 {field1}");
@@ -685,7 +706,8 @@ namespace FreeSql.Extensions.ZeroEntity
}
throw new Exception($"未实现 {@operator}");
}
- public SelectImpl WhereDynamicFilter(DynamicFilterInfo filter)
+ public SelectImpl WhereColumnsIf(bool condition, string field, string @operator, string field2) => condition ? WhereColumns(field, @operator, field2) : this;
+ public SelectImpl WhereDynamicFilter(DynamicFilterInfo filter)
{
var sql = ParseDynamicFilter(filter);
_selectProvider._where.Append(sql);
@@ -778,8 +800,15 @@ namespace FreeSql.Extensions.ZeroEntity
_selectProvider._where.Append($" AND {(notExists ? "NOT " : "")}EXISTS({query.ToSql("1").Replace(" \r\n", " \r\n ")})");
return this;
}
+ ///
+ /// WHERE exists(select ...)
+ ///
+ ///
+ ///
public SelectImpl WhereExists(Func q) => WhereExists(q, false);
+ public SelectImpl WhereExistsIf(bool condition, Func q) => condition ? WhereExists(q, false) : this;
public SelectImpl WhereNotExists(Func q) => WhereExists(q, true);
+ public SelectImpl WhereNotExistsIf(bool condition, Func q) => condition ? WhereExists(q, true) : this;
public SelectImpl GroupByRaw(string sql)
{
if (string.IsNullOrWhiteSpace(sql)) return this;