Click here to Skip to main content
15,878,231 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Class Structure:
public List<customer> CustomerDetails { get; set; }
public class customer
{
[JsonProperty("key")]
public string Key { get; set; }
[JsonProperty("firstAmount")]
public int? FirstAmount{ get; set; }
[JsonProperty("secondAmount")]
public int? SecondAmount{ get; set; }
[JsonProperty("total")]
public int? Total{ get; set; }
}
Response Data:
"Details": {
"Status": "0",
"customer": [
{
"FirstAmount": "796.61",
"SecondAmount": "392.43",
"total": "2",
"key": "Platinum"
},
{
"FirstAmount": "0.00",
"SecondAmount": "0.00",
"total": "0",
"key": "Gold"
},
]
}

Required: *How to select the customer data with Key value "Gold" from the response data. Example need SecondAmount of Customer with Key value "Gold"

What I have tried:

customer.Gold.SecondAmount = CustomerDetails.customer.Select(x => x.SecondAmount).Where(x => x.key.ToString() == ">Gold");
Posted
Updated 18-Aug-22 21:27pm

1 solution

Remove the ">" character to start with:
C#
customer.Gold.SecondAmount = CustomerDetails ... == ">Gold");
                                                     ^
                                                     |
Then change "key" to "Key" to match your class definition - C# is case sensitive, so they are not the same field name!.
You can also remove the ToString call since the Key field is already a string.

Then think about what the Where call will return.
Is it a single item? Or a collection of items?

And since the customer class doesn't have any static members, you would need an instance of the class to assign to, and a Gold field or property to work with!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900