【问题标题】:Add an item in sqlquery result list在 sqlquery 结果列表中添加一项
【发布时间】:2020-05-25 13:29:21
【问题描述】:

我正在使用控制器调用存储过程。

var insert_query = entities.Database.SqlQuery<Call_Info>("exec [dbo].[insert_call_info] @call_id, @user_id, @call_arrive, @call_end, @info",
                        new SqlParameter("call_id", call_id),
                        new SqlParameter("user_id", u_id),
                        new SqlParameter("call_arrive", call_arrive),
                        new SqlParameter("call_end", call_end),
                        new SqlParameter("info", info)
                        ).ToList();
jsonResult = JsonConvert.SerializeObject(insert_query); // <-- using Newtonsoft.Json

有没有办法在 insert_query 列表或 jsonResult 中添加{'StatusCode':1}

我当前的json字符串是:

"{\"call_info_id\":5,\"call_id\":91391,\"user_id\":\"105bdbfb-d65a-42d3-ac79-c1e2575ed243\",\"call_arrive\":\"2020-04-03T21:51:24.797\",\"call_end\":\"2020-04-03T22:04:24.797\",\"info\":\"test\",\"AspNetUser\":null,\"Call\":null}"

新的 json 字符串应该是:

"{\"call_info_id\":5,\"call_id\":91391,\"user_id\":\"105bdbfb-d65a-42d3-ac79-c1e2575ed243\",\"call_arrive\":\"2020-04-03T21:51:24.797\",\"call_end\":\"2020-04-03T22:04:24.797\",\"info\":\"test\",\"AspNetUser\":null,\"Call\":null, \"StatusCode\":1}"

【问题讨论】:

  • 请使用示例 json 输出更新问题。你拥有什么,你想要什么。
  • 现在,您可能正在获取一个 JSON 数组,是吗? (即[ {...}, {...} ])所以:状态码在哪里?你有一个你想要的示例输出吗?
  • @MarcGravell 我已经添加了。

标签: c# json asp.net-mvc list


【解决方案1】:

例如:

var insert_query = entities.Database.SqlQuery<Call_Info>("exec [dbo].[insert_call_info] @call_id, @user_id, @call_arrive, @call_end, @info",
                        new SqlParameter("call_id", call_id),
                        new SqlParameter("user_id", u_id),
                        new SqlParameter("call_arrive", call_arrive),
                        new SqlParameter("call_end", call_end),
                        new SqlParameter("info", info))
.Select(x => new {
   // Write all the properties that you want from the result
   x.call_info_id,
   x.call_id,
   x.user_id,
   x.call_arrive,
   x.call_end,
   x.info,
   x.AspNetUser,
   x.Call,
   // add the extra field 
   StausCode = 1
})
.ToList();

或者:

foreach(JObject obj in (jsonResult as JArray))
{
   obj["StatusCode"] = 1;
}

或者,如果可以的话,将属性添加到类 Call_Info 可能是最简单的方法

public int SatusCode => 1;

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2018-02-06
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多