SqlKata根据IEnumerable <KeyValuePair <字符串,对象>>或List <KeyValuePair <字符串,对象>>更新列

时间:2019-08-08 06:03:35

标签: c# keyvaluepair sqlkata

如何使用List>(其中string =列名和对象=列值)或IEnumerable>(string =列,object =列值)作为SQLKata更新语句的更新部分?我在以下方面遇到了麻烦。我收到“参数计数不匹配”(但日志未向我显示它正在尝试建立的查询)。

这是我的代码。基本上,我创建了一组键值对,然后删除其中一个键值对,因为它是主键(我不想更新它,我只想更新所有其他列)。最后,我希望能够使用Update(t)(其中t是我的每个列及其对应列值的表示,例如Column =“ value”,Column1 =“ anothervalue”等)

                foreach (dynamic d in r.Data)
                {
                    JObject o = d;

                    // Build result containing columns and column values
                    var result = o.Descendants()
                                .OfType<JProperty>()
                                .Select(p => new KeyValuePair<string, object>(p.Path,
                                p.Value.Type == JTokenType.Array || p.Value.Type == JTokenType.Object
                                ? null : p.Value));

                    // Create a list so we can remove the primary key from the actual update
                    var t = new List<KeyValuePair<string, object>>();

                    t = result.ToList();

                    context.Logger.LogLine($"Current columns without removal: {JsonConvert.SerializeObject(t,Formatting.Indented)}");

                    // Remove the key value pair that represents the primary key as we dont want to update that.
                    foreach (var re in result)
                    {
                        if (re.Key == r.ReferencePrimaryKey)
                        {
                            t.Remove(re);
                        }
                    }

                    context.Logger.LogLine($"Current columns with removal: {JsonConvert.SerializeObject(t, Formatting.Indented)}");                          

                    db.Logger = compiled =>
                    {
                        context.Logger.Log($"{compiled.ToString()}");
                    };

                    int affected = db.Query(r.TableRef).Where("STORE_ID", state.StoreId).Where(r.ReferencePrimaryKey, r.PrimaryKeyId).Update(t);
                    context.Logger.LogLine($"affected rows: {affected}");
                }

编辑:这是键值对,我删除了ITM_ID作为其主键,并且我不想更新它。

{
    "Key": "ITM_ID",
    "Value": 7000901
}
,
{
    "Key": "INTRNL_ID",
    "Value": 7000901
}
,
{
    "Key": "MD_FG",
    "Value": false
}
,
{
    "Key": "COST_CASE_PRC",
    "Value": 7.5
}
,
{
    "Key": "UNIT_CASE",
    "Value": 1
}

0 个答案:

没有答案