【问题标题】:How to manipulate the WCF service result?如何操作 WCF 服务结果?
【发布时间】:2014-09-16 13:30:15
【问题描述】:

我有一个使用 VS AppBuilder 扩展开发的剑道移动应用程序。我创建了一个 WCF 服务。我正在使用此服务将数据与剑道图表绑定。这是我的 WCF 服务代码。

 public List<object> ProductCount(int week, int year)
    {
        List<object> lst = new List<object>();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(connString);
        var currentCulture = CultureInfo.CurrentCulture;
        var weekNo = currentCulture.Calendar.GetWeekOfYear(
                        DateTime.Now.Date,
                        currentCulture.DateTimeFormat.CalendarWeekRule,
                        currentCulture.DateTimeFormat.FirstDayOfWeek);
        try
        {
            SqlCommand cmd = new SqlCommand("Select * from Products where Week_Number =" + week_No);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {
                lst.Add(dr["Quantity"].ToString());
            }
            int current = lst.Sum(x => Convert.ToInt32(x)); //result is 5000
            int goal = 10000;
            lst.Clear();
            dt.Clear();
            lst.Add("current:" + current);
            lst.Add("target:" + goal);
        }
        catch (Exception ex)
        {
            // new Error(ex);
        }
        finally
        {
            con.Close();
        }
        return lst;
    }

此代码返回这样的结果, [“当前:5000”,“目标:10000”]

但我想要这样的结果, [{“当前”:5000,“目标”:10000}]

我该怎么做?

【问题讨论】:

  • 您是否希望将结果序列化为 JSON?
  • 切换到Dictionary&lt;string,int&gt; 或者可能是Ordered dictionary
  • 添加 json 序列化程序后,它给了我以下结果:"[\"current:5000\",\"target:10000\"]" 但我想要这样,[{"current ":5000, "目标":10000}]

标签: c# wcf-binding kendo-mobile


【解决方案1】:

你试过了吗:

lst.Add(new {
    current = current,
    target = goal
});

代替:

lst.Add("current:" + current);
lst.Add("target:" + goal);

这会在您的数组中添加一个具有currenttarget 属性的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多