Skip to content

Commit 4ea657d

Browse files
committed
- ZeroDbContext 补充 WhereIf;
1 parent 8c5f367 commit 4ea657d

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

Extensions/FreeSql.Extensions.ZeroEntity/ZeroDbContext.SelectImpl.cs

+38-9
Original file line numberDiff line numberDiff line change
@@ -589,26 +589,37 @@ public SelectImpl Where(IEnumerable<T> items)
589589
/// WHERE [field] IN (...)
590590
/// </summary>
591591
public SelectImpl WhereIn(string field, object value) => Where(field, "in", value);
592+
public SelectImpl WhereInIf(bool condition, string field, object value) => condition ? Where(field, "in", value) : this;
592593
/// <summary>
593594
/// WHERE [field] NOT IN (...)
594595
/// </summary>
595596
public SelectImpl WhereNotIn(string field, object value) => Where(field, "!in", value);
597+
public SelectImpl WhereNotInIf(bool condition, string field, object value) => condition ? Where(field, "!in", value) : this;
596598
/// <summary>
597-
/// Where(new { Year = 2017, CategoryId = 198, IsPublished = true })<para></para>
598-
/// WHERE [Year] = 2017 AND [CategoryId] = 198 AND [IsPublished] = 1
599+
/// 匿名对象:Where(new { Year = 2017, CategoryId = 198, IsPublished = true })<para></para>
600+
/// 字典对象:Where(new Dictionary&lt;string, object&gt; { ["Year"] = 2017, ["CategoryId"] = 198, ["IsPublished"] = true })<para></para>
601+
/// WHERE [Year] = 2017 AND [CategoryId] = 198 AND [IsPublished] = 1<para></para>
599602
/// </summary>
600-
public SelectImpl Where(object multipleFields)
603+
public SelectImpl WhereDynamic(object dywhere)
601604
{
602-
if (multipleFields == null) return this;
603-
foreach (var prop in multipleFields.GetType().GetProperties())
604-
WhereDynamicFilter(new DynamicFilterInfo { Field = prop.Name, Operator = DynamicFilterOperator.Eq, Value = prop.GetValue(multipleFields, null) });
605+
if (dywhere == null) return this;
606+
if (dywhere is Dictionary<string, object> dict)
607+
{
608+
foreach(var kv in dict)
609+
WhereDynamicFilter(new DynamicFilterInfo { Field = kv.Key, Operator = DynamicFilterOperator.Eq, Value = kv.Value });
610+
return this;
611+
}
612+
foreach (var prop in dywhere.GetType().GetProperties())
613+
WhereDynamicFilter(new DynamicFilterInfo { Field = prop.Name, Operator = DynamicFilterOperator.Eq, Value = prop.GetValue(dywhere, null) });
605614
return this;
606615
}
616+
public SelectImpl WhereDynamicIf(bool condition, object dywhere) => condition ? WhereDynamic(dywhere) : this;
607617
/// <summary>
608618
/// WHERE [field] = ..<para></para>
609619
/// 更全请看重载 Where(string field, string @operator, object value)
610620
/// </summary>
611621
public SelectImpl Where(string field, object value) => WhereDynamicFilter(new DynamicFilterInfo { Field = field, Operator = DynamicFilterOperator.Eq, Value = value });
622+
public SelectImpl WhereIf(bool condition, string field, object value) => condition ? Where(field, value) : this;
612623
/// <summary>
613624
/// WHERE [field] <para></para>
614625
/// !=、&gt;、&gt;=、&lt;、&lt;=、like、!like、in、!in<para></para>
@@ -652,8 +663,18 @@ public SelectImpl Where(string field, string @operator, object value)
652663
return WhereDynamicFilter(new DynamicFilterInfo { Field = field, Operator = DynamicFilterOperator.NotAny, Value = value });
653664
}
654665
throw new Exception($"未实现 {@operator}");
655-
}
656-
public SelectImpl WhereColumns(string field1, string @operator, string field2)
666+
}
667+
public SelectImpl WhereIf(bool condition, string field, string @operator, object value) => condition ? Where(field, @operator, value) : this;
668+
669+
/// <summary>
670+
/// WHERE 字段与字段
671+
/// </summary>
672+
/// <param name="field1"></param>
673+
/// <param name="operator"></param>
674+
/// <param name="field2"></param>
675+
/// <returns></returns>
676+
/// <exception cref="Exception"></exception>
677+
public SelectImpl WhereColumns(string field1, string @operator, string field2)
657678
{
658679
var field1Result = ParseField(null, null, field1);
659680
if (field1Result == null) throw new Exception($"未匹配字段名 {field1}");
@@ -685,7 +706,8 @@ public SelectImpl WhereColumns(string field1, string @operator, string field2)
685706
}
686707
throw new Exception($"未实现 {@operator}");
687708
}
688-
public SelectImpl WhereDynamicFilter(DynamicFilterInfo filter)
709+
public SelectImpl WhereColumnsIf(bool condition, string field, string @operator, string field2) => condition ? WhereColumns(field, @operator, field2) : this;
710+
public SelectImpl WhereDynamicFilter(DynamicFilterInfo filter)
689711
{
690712
var sql = ParseDynamicFilter(filter);
691713
_selectProvider._where.Append(sql);
@@ -778,8 +800,15 @@ SelectImpl WhereExists(Func<SubQuery, SelectImpl> q, bool notExists)
778800
_selectProvider._where.Append($" AND {(notExists ? "NOT " : "")}EXISTS({query.ToSql("1").Replace(" \r\n", " \r\n ")})");
779801
return this;
780802
}
803+
/// <summary>
804+
/// WHERE exists(select ...)
805+
/// </summary>
806+
/// <param name="q"></param>
807+
/// <returns></returns>
781808
public SelectImpl WhereExists(Func<SubQuery, SelectImpl> q) => WhereExists(q, false);
809+
public SelectImpl WhereExistsIf(bool condition, Func<SubQuery, SelectImpl> q) => condition ? WhereExists(q, false) : this;
782810
public SelectImpl WhereNotExists(Func<SubQuery, SelectImpl> q) => WhereExists(q, true);
811+
public SelectImpl WhereNotExistsIf(bool condition, Func<SubQuery, SelectImpl> q) => condition ? WhereExists(q, true) : this;
783812
public SelectImpl GroupByRaw(string sql)
784813
{
785814
if (string.IsNullOrWhiteSpace(sql)) return this;

0 commit comments

Comments
 (0)