01.
public
static
class
clsMyFilter
02.
{
03.
private
static
string
CommaSeparated(IEnumerable<
string
> list) =>
string
.Join(
","
, list);
04.
private
static
string
SeparatedByAndStatement(IEnumerable<
string
> list) =>
string
.Join(
" AND "
, list);
05.
public
static
async Task Update(
this
IDbConnection con,
string
tableName,
object
identityParameters,
object
parametersObject)
06.
{
07.
IEnumerable<PropertyInfo> propertyInfo = parametersObject.GetType().GetProperties();
08.
IEnumerable<PropertyInfo> identityPropertyInfo = identityParameters.GetType().GetProperties();
09.
IEnumerable<
string
> columnsEqualToParameter = propertyInfo.Select(p => $
"[{p.Name}] = @{p.Name}"
);
10.
IEnumerable<
string
> identityColumnsEqualToParameter = identityPropertyInfo.Select(p => $
"[{p.Name}] = @{p.Name}"
);
11.
12.
string
sql =
new
StringBuilder()
13.
.Append(
"UPDATE "
+ tableName +
" SET "
)
14.
.Append(CommaSeparated(columnsEqualToParameter))
15.
.Append($
" WHERE "
)
16.
.Append(SeparatedByAndStatement(identityColumnsEqualToParameter))
17.
.Append(
";"
)
18.
.ToString();
19.
20.
IDictionary<
string
,
object
> allParameters =
new
ExpandoObject();
21.
22.
foreach
(PropertyInfo info
in
propertyInfo)
23.
allParameters.Add(info.Name, parametersObject.GetType().GetProperty(info.Name).GetValue(parametersObject));
24.
25.
foreach
(PropertyInfo info
in
identityPropertyInfo)
26.
allParameters.Add(info.Name, identityParameters.GetType().GetProperty(info.Name).GetValue(identityParameters));
27.
28.
await con.ExecuteAsync(sql, allParameters);
29.
}
30.
}