From a0acece7e504247e9970721ce48a355a00874cc8 Mon Sep 17 00:00:00 2001
From: 28810 <28810@YEXIANGQIN>
Date: Wed, 13 May 2020 16:52:12 +0800
Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20IUpdate.SetIf=20?=
=?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=9B#309?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FreeSql/Interface/Curd/IUpdate.cs | 19 +++++++++++++++++++
.../Internal/CommonProvider/UpdateProvider.cs | 2 ++
2 files changed, 21 insertions(+)
diff --git a/FreeSql/Interface/Curd/IUpdate.cs b/FreeSql/Interface/Curd/IUpdate.cs
index 41a8758b..289f3096 100644
--- a/FreeSql/Interface/Curd/IUpdate.cs
+++ b/FreeSql/Interface/Curd/IUpdate.cs
@@ -93,6 +93,15 @@ namespace FreeSql
///
IUpdate Set(Expression> column, TMember value);
///
+ /// 设置列的新值,Set(a => a.Name, "newvalue")
+ ///
+ ///
+ /// true 时生效
+ /// lambda选择列
+ /// 新值
+ ///
+ IUpdate SetIf(bool condition, Expression> column, TMember value);
+ ///
/// 设置列的的新值为基础上增加,格式:Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
///
/// 指定更新,格式:Set(a => new T { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
@@ -102,6 +111,16 @@ namespace FreeSql
///
IUpdate Set(Expression> exp);
///
+ /// 设置列的的新值为基础上增加,格式:Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
+ ///
+ /// 指定更新,格式:Set(a => new T { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
+ ///
+ ///
+ /// true 时生效
+ ///
+ ///
+ IUpdate SetIf(bool condition, Expression> exp);
+ ///
/// 设置值,自定义SQL语法,SetRaw("title = ?title", new { title = "newtitle" })
///
/// sql语法
diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs
index 4f85e510..1a7cf8ab 100644
--- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs
+++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs
@@ -400,6 +400,7 @@ namespace FreeSql.Internal.CommonProvider
SetPriv(cols.First().Column, value);
return this;
}
+ public IUpdate SetIf(bool condition, Expression> column, TMember value) => condition ? Set(column, value) : this;
public IUpdate Set(Expression> exp)
{
var body = exp?.Body;
@@ -459,6 +460,7 @@ namespace FreeSql.Internal.CommonProvider
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(cols.First().Column.Attribute.Name)).Append(" = ").Append(expt);
return this;
}
+ public IUpdate SetIf(bool condition, Expression> exp) => condition ? Set(exp) : this;
public IUpdate SetRaw(string sql, object parms = null)
{
if (string.IsNullOrEmpty(sql)) return this;