Merge pull request #1688 from easy999000/easy999000

添加 通过DTO 进行update的方法, 特点是,忽略值为null的属性.
This commit is contained in:
2881099 2023-12-14 20:08:45 +08:00 committed by GitHub
commit 25e8a3e778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -777,6 +777,38 @@ namespace FreeSql.Internal.CommonProvider
} }
return this; return this;
} }
public IUpdate<T1> SetDtoIgnoreNull(object dto)
{
if (dto == null) return this;
if (dto is Dictionary<string, object>)
{
var dic = dto as Dictionary<string, object>;
foreach (var kv in dic)
{
if (kv.Value == null)
{
continue;
}
if (_table.ColumnsByCs.TryGetValue(kv.Key, out var trycol) == false) continue;
if (_ignore.ContainsKey(trycol.Attribute.Name)) continue;
SetPriv(trycol, kv.Value);
}
return this;
}
var dtoProps = dto.GetType().GetProperties();
foreach (var dtoProp in dtoProps)
{
var v3 = dtoProp.GetValue(dto, null);
if (v3 == null)
{
continue;
}
if (_table.ColumnsByCs.TryGetValue(dtoProp.Name, out var trycol) == false) continue;
if (_ignore.ContainsKey(trycol.Attribute.Name)) continue;
SetPriv(trycol, v3);
}
return this;
}
public IUpdate<T1> Where(Expression<Func<T1, bool>> exp) => WhereIf(true, exp); public IUpdate<T1> Where(Expression<Func<T1, bool>> exp) => WhereIf(true, exp);
public IUpdate<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp) public IUpdate<T1> WhereIf(bool condition, Expression<Func<T1, bool>> exp)