0

我有以下代码,它采用像这样的动态对象:(基础类型 JObject,字段可以不同,但​​它始终是 JObject,而不是数组或其他任何东西)

{
    "TRS_KEY": 1720,
    "TRS_PTR": 11,
    "TicTtl": false,
    "VoidTic": false,
    "SaveTrs": false,
    "RclTrs": false,
    "DrvOff": false,
    "QuickStore": false,
    "InfGTtl": true,
    "TndrXchng": false,
    "CpnTic": false,
    "TndrTic": false,
    "IHPay": false,
    "IHPurch": false,
    "SendEmail": false,
    "TicetID": 577,
    "TaxVal": 0,
    "Items": 0,
    "Amount": 0,
    "DiscAmt": 0,
    "AmountNT": 0,
    "DiscNT": 0,
    "GTtlAmt": 9088.9,
    "RetnID": 0,
    "FSPmt": 0,
    "AmtEleCpn": 0,
    "ZCnt": 0,
    "TailFlags": 8,
    "MmbrFirst": false
}

并循环遍历每一个,将字段名称转换为列,并将值转换为创建 SQL INSERT 的值。编码:

foreach (dynamic d in r.Data)
{
    //context.Logger.LogLine($"{d.GetType()}");
    try
    {
        context.Logger.LogLine($"{r.TableRef} {JsonConvert.SerializeObject(d)}");

        JObject o = d;
        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));

        IEnumerable<string> columns = result.Select(p => p.Key);
        IEnumerable<object> values = result.Select(p => p.Value);

        // Add custom columns
        IEnumerable<string> colAdded = columns.Add("STORE_ID").Add("last_modified");

        IEnumerable<object> valAdded = values.Add(state.StoreId).Add(DateTime.Now);

        foreach (var v in valAdded)
        {
            context.Logger.LogLine($"{JsonConvert.SerializeObject(v)}");
        }

        var queryResult = db.Query(r.TableRef).AsInsert(colAdded, valAdded);
        queryResult.Get();

        context.Logger.LogLine($"COLUMNS {JsonConvert.SerializeObject(columns,Formatting.Indented)}");
        context.Logger.LogLine($"VALUES {JsonConvert.SerializeObject(values, Formatting.Indented)}");

        //var columns = result.Select(pairs => result.Select(pair => pair.Key).ToArray().ToString());
        //context.Logger.LogLine($"COLUMNS {JsonConvert.SerializeObject(columns)}");
        //var values = result.Select(pairs => result.Select(pair => pair.Value).ToArray());
        //context.Logger.LogLine($"VALUES TO GO INTO INSERTS {JsonConvert.SerializeObject(values)}");
        //var queryResult = db.Query("S2C").Insert(new { TRS_KEY = "3422", InsertedDateTime = DateTime.Now });
        //var queryResult = db.Query(r.TableRef).Insert(columns, values);

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

但是,当我将这些对象传递给 SqlKata 以执行此操作INSERT并将编译后的查询记录到日志中时,如您所见,这些值都是空的,所以它几乎是整个过程,除了两个我在运行查询之前手动添加的值。那些看起来没问题。

INSERT INTO [TLog050000TicketEnd] ([TRS_KEY], [TRS_PTR], [TicTtl],
                                   [VoidTic], [SaveTrs], [RclTrs], [DrvOff], [QuickStore], [InfGTtl],
                                   [TndrXchng], [CpnTic], [TndrTic], [IHPay], [IHPurch], [SendEmail],
                                   [TicetID], [TaxVal], [Items], [Amount], [DiscAmt], [AmountNT],
                                   [DiscNT], [GTtlAmt], [RetnID], [FSPmt], [AmtEleCpn], [ZCnt],
                                   [TailFlags], [MmbrFirst], [STORE_ID], [last_modified]) 
VALUES (, , , ,
        , , , , , , , , , , , , , , , , , , , , , , , , , 
        'A777', '2019-08-03 20:11:12')

现在列 ( IEnumerable<string> columns) 很好,如您所见,我手动添加的两个值也显示了,但是放入IEnumerable<object>values 中(然后复制到valAdded)的所有值根本不显示?

我不明白为什么。

IEnumerable当我输出到日志时,我什至循环并可以看到其中的每个值,例如

foreach (var v in valAdded)
{
    context.Logger.LogLine($"{JsonConvert.SerializeObject(v)}");
}

输出:

1720.0 11 false false false false false false true false false false false false false 577 0.0 0 0.0 0.0 0.0

谁能帮我找出我在这里出错的地方?:( 我吓坏了。

4

0 回答 0