- 优化 IUpdate.Set 表达式传入匿名类更新多个字段;

This commit is contained in:
28810
2019-07-26 14:30:03 +08:00
parent 7460ffffaa
commit 4609c910dd
5 changed files with 43 additions and 24 deletions

View File

@ -1568,7 +1568,7 @@
<summary>
设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
<para></para>
指定更新格式Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
指定更新格式Set(a => new T { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
</summary>
<typeparam name="TMember"></typeparam>
<param name="exp"></param>

View File

@ -77,7 +77,7 @@ namespace FreeSql
/// <summary>
/// 设置列的的新值为基础上增加格式Set(a => a.Clicks + 1) 相当于 clicks=clicks+1
/// <para></para>
/// 指定更新格式Set(a => new { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
/// 指定更新格式Set(a => new T { Clicks = a.Clicks + 1, Time = DateTime.Now }) 相当于 set clicks=clicks+1,time='2019-06-19....'
/// </summary>
/// <typeparam name="TMember"></typeparam>
/// <param name="exp"></param>

View File

@ -435,29 +435,41 @@ namespace FreeSql.Internal.CommonProvider
{
var body = exp?.Body;
var nodeType = body?.NodeType;
if (nodeType == ExpressionType.Equal)
switch (nodeType)
{
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null));
return this;
}
if (nodeType == ExpressionType.MemberInit)
{
var initExp = body as MemberInitExpression;
if (initExp.Bindings?.Count > 0)
{
for (var a = 0; a < initExp.Bindings.Count; a++)
case ExpressionType.Equal:
_set.Append(", ").Append(_commonExpression.ExpressionWhereLambdaNoneForeignObject(null, _table, null, exp, null));
return this;
case ExpressionType.MemberInit:
var initExp = body as MemberInitExpression;
if (initExp.Bindings?.Count > 0)
{
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
if (initAssignExp == null) continue;
var memberName = initExp.Bindings[a].Member.Name;
if (_table.ColumnsByCsIgnore.ContainsKey(memberName)) continue;
if (_table.ColumnsByCs.TryGetValue(memberName, out var col) == false) throw new Exception($"找不到属性:{memberName}");
var memberValue = _commonExpression.ExpressionLambdaToSql(initAssignExp.Expression, new CommonExpression.ExpTSC { });
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(memberValue);
for (var a = 0; a < initExp.Bindings.Count; a++)
{
var initAssignExp = (initExp.Bindings[a] as MemberAssignment);
if (initAssignExp == null) continue;
var memberName = initExp.Bindings[a].Member.Name;
if (_table.ColumnsByCsIgnore.ContainsKey(memberName)) continue;
if (_table.ColumnsByCs.TryGetValue(memberName, out var col) == false) throw new Exception($"找不到属性:{memberName}");
var memberValue = _commonExpression.ExpressionLambdaToSql(initAssignExp.Expression, new CommonExpression.ExpTSC { });
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(memberValue);
}
}
}
return this;
return this;
case ExpressionType.New:
var newExp = body as NewExpression;
if (newExp.Members?.Count > 0)
{
for (var a = 0; a < newExp.Members.Count; a++)
{
var memberName = newExp.Members[a].Name;
if (_table.ColumnsByCsIgnore.ContainsKey(memberName)) continue;
if (_table.ColumnsByCs.TryGetValue(memberName, out var col) == false) throw new Exception($"找不到属性:{memberName}");
var memberValue = _commonExpression.ExpressionLambdaToSql(newExp.Arguments[a], new CommonExpression.ExpTSC { });
_setIncr.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ").Append(memberValue);
}
}
return this;
}
if (body is BinaryExpression == false &&
nodeType != ExpressionType.Call) return this;