[HttpGet]
public object SelectToken_LinQ()
{
JObject o = JObject.Parse(@"{
'SaleName':'Peter','Age':31,
'Stores': ['Lambton Quay','Willis Street'],
'Manufacturers': [
{'Name': 'Acme Co','Products': [{'Name': 'Anvil','Price': 50}]},
{'Name': 'Contoso','Products': [{'Name': 'Elbow Grease','Price': 99.95},
{'Name': 'Headlight Fluid','Price': 4}]}]
}");
IList<string> storeNames = o.SelectToken("Stores").Select(s => (string)s).ToList();
string storeNames_List = string.Join(",", storeNames);
// Lambton Quay
// Willis Street
IList<string> firstProductNames = o["Manufacturers"].Select(m => (string)m.SelectToken("Products[1].Name")).ToList();
string firstProductNames_List = string.Join(",", firstProductNames);
// null
// Headlight Fluid
decimal totalPrice = o["Manufacturers"].Sum(m => (decimal)m.SelectToken("Products[0].Price"));
// 149.95
return new { storeNames_List , firstProductNames_List , totalPrice };
}
0 Nhận xét