如何连接结果Json(new{lstrng,a}, JsonRequestBehavior.AllowGet);
。我想一起查看lstrng和a的结果。我的代码是
var a = this.abc();
JsonResult json =Json(new{lstrng,a}, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;
这里abc是另一种方法,而lstrng是当前方法的结果
public JsonResult gFbrRecord()
{
DataSet ds = dblayer.gfbrdata();
//this is for getordert tart
List<fbrData> lstrng = new List<fbrData>(10000);
foreach (DataRow dr in ds.Tables[0].Rows)
{
lstrng.Add(new fbrData
{
FBRInvoiceNumber = dr["FBR_NUM"] != DBNull.Value ? Convert.ToString(dr["FBR_NUM"]) : Convert.ToString(""),
POSID = dr["REG_NUM"] != DBNull.Value ? Convert.ToInt32(dr["REG_NUM"]) : Convert.ToInt32(0),
});
}
var a = this.abc();
JsonResult json =Json(new{lstrng,a}, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;
}
public JsonResult abc() {
DataSet ds = dblayer.gfbrdata();
List<fbr1> lstrng = new List<fbr1>(10000);
foreach (DataRow dr in ds.Tables[0].Rows)
{
lstrng.Add(new fbr1
{
DetailSerialNo = dr["SRL_NUM"] != DBNull.Value ? Convert.ToInt32(dr["SRL_NUM"]) : Convert.ToInt32(0),
ItemCode = dr["ITM_COD"] != DBNull.Value ? Convert.ToString(dr["ITM_COD"]) : Convert.ToString(""),
});
}
JsonResult json = Json(lstrng, JsonRequestBehavior.AllowGet);
json.MaxJsonLength = int.MaxValue;
return json;
}
}
答案 0 :(得分:2)
您需要查询两个列表并从第二个列表中选择多个项目, 与第一个列表中的单个项目具有相同的ID
public JsonResult gFbrRecord()
{
DataSet ds1 = dblayer.gfbrdata();
List<fbrData> lstrng1 = new List<fbrData>(10000);
//Your other stuff here
DataSet ds2 = dblayer.gfbrdata();
List<fbr1> lstrng2 = new List<fbr1>(10000);
//Your other stuff here
var result = (from item1 in lstrng1
select new
{
FBRInvoiceNumber = item1.FBRInvoiceNumber,
POSID = item1.POSID,
SRL_NUM = item1.SRL_NUM,
Data = (from item2 in lstrng2
where item2.SRL_NUM == item1.SRL_NUM
select new
{
DetailSerialNo = item2.DetailSerialNo,
ItemCode = item2.ItemCode
}).ToList()
}).ToList();
return Json(result, "application/json", JsonRequestBehavior.AllowGet);
}
替代
基本上,我为您提供了一个示例代码,该代码应适用于两个json对象的串联
1)此方法从数据库中获取记录并填充列表,然后从该列表中创建JToken
public static JToken Method1()
{
DataSet ds = dblayer.gfbrdata();
List<fbrData> lstrng = new List<fbrData>(10000);
//Your other stuff here
JToken jToken = JToken.FromObject(lstrng);
return jToken;
}
JToken
通过上述方法返回的样子
[
{
"FBRInvoiceNumber": "ABC",
"POSID": 110034,
"SRL_NUM": 123
},
{
"FBRInvoiceNumber": "PQR",
"POSID": 210035,
"SRL_NUM": 456
}
]
2)此方法也与上述方法相同,只是区别在于该方法是从数据库中获取除上述方法之外的记录。
public static JToken Method2()
{
DataSet ds = dblayer.gfbrdata();
List<fbr1> lstrng = new List<fbr1>(10000);
//Your other stuff here
JToken jToken = JToken.FromObject(lstrng);
return jToken;
}
JToken
通过上述方法返回的样子
[
{
"DetailSerialNo": 8,
"ItemCode": "700275",
"SRL_NUM": 123
},
{
"DetailSerialNo": 9,
"ItemCode": "800279",
"SRL_NUM": 456
}
]
3)现在,以下函数可以将两个数组中的每个对象连接到同一索引上。
自从您在上述评论中提到
您要通过相同的属性键
SRL_NUM
值将两个数组中的每个对象串联起来。
public static JToken Concat(JToken jToken1, JToken jToken2)
{
JArray jArray = new JArray();
foreach (JObject jObject1 in jToken1.ToObject<JArray>())
{
var value1 = jObject1.Properties().Where(x => x.Name == "SRL_NUM").FirstOrDefault().Value;
foreach (JObject jObject2 in jToken2.ToObject<JArray>())
{
var value2 = jObject2.Properties().Where(x => x.Name == "SRL_NUM").FirstOrDefault().Value;
if (Convert.ToInt32(value1) == Convert.ToInt32(value2))
{
jObject1.Merge(jObject2, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });
jArray.Add(jObject1);
}
}
}
return jArray;
}
4)然后,您可以将上述方法用作您的操作方法
public ContentResult gFbrRecord()
{
JToken jToken1 = Method1();
JToken jToken2 = Method2();
var result = Concat(jToken1, jToken2);
return Content(result.ToString(), "application/json");
}
输出:
编辑1:
如果您的数组在第二个数组中包含多个SRL_NUM
,则以下方法可以将所有具有相同SRL_NUM
的项与具有相同SRL_NUM
的第一个数组项并置。
public static JToken Concat(JToken jToken1, JToken jToken2)
{
JArray jArray = new JArray();
foreach (JObject jObject1 in jToken1.ToObject<JArray>())
{
var value = jObject1.Properties().Where(x => x.Name == "SRL_NUM").FirstOrDefault().Value;
var result = jToken2.ToObject<JArray>().ToObject<JObject[]>().Properties().Where(x => x.Name == "SRL_NUM" && Convert.ToInt32(x.Value) == Convert.ToInt32(value)).Select(x => x.Parent);
JArray jAr = new JArray();
foreach (JObject obj in result)
{
jAr.Add(obj);
}
JObject jObject = new JObject();
jObject.Merge(jObject1, new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling.Concat });
jObject.Add("Data", jAr);
jArray.Add(jObject);
}
return jArray;
}
输出:
答案 1 :(得分:0)
我想您只希望list的结果仅合并a和lstrng ??
a.Data.AddRang(lstrng.Data)
该代码将arr的两个合并,您需要在lstrng模型中定义a.Data的列表。