C# 如何操作WCF服务结果?
我有一个使用VS AppBuilder扩展开发的剑道移动应用程序。我已经创建了一个WCF服务。我正在使用此服务将数据与剑道图绑定。这是我的WCF服务代码C# 如何操作WCF服务结果?,c#,wcf-binding,kendo-mobile,C#,Wcf Binding,Kendo Mobile,我有一个使用VS AppBuilder扩展开发的剑道移动应用程序。我已经创建了一个WCF服务。我正在使用此服务将数据与剑道图绑定。这是我的WCF服务代码 public List<object> ProductCount(int week, int year) { List<object> lst = new List<object>(); DataSet ds = new DataSet(); Data
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;
}
public List ProductCount(整数周、整数年)
{
List lst=新列表();
数据集ds=新数据集();
DataTable dt=新的DataTable();
SqlConnection con=新的SqlConnection(connString);
var currentCulture=CultureInfo.currentCulture;
var weekNo=currentCulture.Calendar.GetWeekOfYear(
DateTime.Now.Date,
currentCulture.DateTimeFormat.CalendarWeekRule,
currentCulture.DateTimeFormat.FirstDayOfWeek);
尝试
{
SqlCommand cmd=new SqlCommand(“从产品中选择*,其中周号=”+周号);
cmd.CommandType=CommandType.Text;
cmd.Connection=con;
SqlDataAdapter da=新的SqlDataAdapter(cmd);
da.填充(dt);
foreach(数据行dr在dt.行中)
{
添加(dr[“Quantity”].ToString());
}
int current=lst.Sum(x=>Convert.ToInt32(x));//结果是5000
int目标=10000;
lst.Clear();
dt.Clear();
添加(“当前:+当前”);
添加(“目标:+目标”);
}
捕获(例外情况除外)
{
//新错误(ex);
}
最后
{
con.Close();
}
返回lst;
}
这段代码返回如下结果,
[“当前:5000”,“目标:10000”]
但我希望我的结果是这样的,
[{“当前”:5000,“目标”:10000}]
我该怎么做 您是否尝试过:
lst.Add(new {
current = current,
target = goal
});
而不是:
lst.Add("current:" + current);
lst.Add("target:" + goal);
这将向数组中添加一个具有
current
和target
属性的对象。是否希望将结果序列化为JSON?切换到字典
或可能是添加JSON序列化程序后,它会给我以下结果:“[\“current:5000\,\“target:10000\”]”但我希望这样[{“当前”:5000,“目标”:10000}]