I am trying to search contacts (Objects) that contains a List<Dictionary<string, string>> by a dictionary key and value
Below is my Json Array of Contacts
[
{
"first_name": "David",
"last_name": "Smith",
"email": "[email protected]",
"phone": "1234567890",
"website": "google.com",
"relations": [
{
"listid": "65512fe1e759b98f40b48829"
},
{
"listid": "34212fe1e759b98f40b48829"
}
]
},
{
"first_name": "Chris",
"last_name": "Oven",
"email": "[email protected]",
"phone": "1234567890",
"website": "google.com",
"relations": [
{
"listid": "65512fe1e759b98f40b48829"
},
{
"listid": "34212fe1e759b98f40b48829"
}
]
}
]
I am trying to find all contacts that contain listid = "65512fe1e759b98f40b48829".
What is the best way to do this using Linq?
Tried something like below:
var searchPair = new KeyValuePair<string, string>("listid", "65512fe1e759b98f40b48829");
contacts.Where(p=>p.relations.Where(dictionary => dictionary[searchPair.Key].ToString().Contains(searchPair.Value)).ToList();
but somewhere it is giving incorrect way for searching
It seems to me that this is a better representation of your JSON:
Now you can read in your data:
That gives:
Now querying it is simple:
Here's the query if you use the dictionary version of
relationsyou suggested: