Currently I am facing an issue to create a LINQ statement which will generate the list of objects that I want. The following section is an example of the LINQ object that i want to process.
{
"successful": "true",
"result": [
[
{
"Param1": "A1",
"Param2": "A2",
"Param3": "A3",
"Param4": "A4",
"Param5": "1",
"Param6": "A5",
},
{
"Param1": "B1",
"Param2": "B2",
"Param3": "B3",
"Param4": "B4",
"Param5": "2",
"Param6": "B5",
},
{
"Param1": "C1",
"Param2": "C2",
"Param3": "C3",
"Param4": "C4",
"Param5": "2",
"Param6": "C5",
}
]
]
}
I have a custom object class as follow,
public class CContainer
{
public string param1{ get; set; }
public string param2{ get; set; }
public string param3{ get; set; }
}
My end goal is to create a list of CContainer objects, that contain only the first 3 parameters (Param1, Param2, and Param3) for each item under the 'result' category. Also, I would like to select only item which its Param5 == "2". I am currently unable to do that using LINQ, please advice.
The following snippet don't work (even if I remove the 'Where' clause).
List<CContainer> testList = new List<CContainer>();
string responseRet = await response.Content.ReadAsStringAsync();
JObject o = JObject.Parse(responseRet);
testList =
(from item in o["result"]
where item["Param5"].Value<string>() == "2"
select new CCOntainer
{
param1 = item["Param1"].Value<string>(),
param2 = item["Param2"].Value<string>(),
param3 = item["Param3"].Value<string>(),
}).ToList();
resultsproperty is an array with a single element... and that single element is itself an array of yourCContainerobject? Is that a mistake or intentional?