I have a JArray, read from a file :
private void RemoveCatalog(Catalog catalog) {
System.IO.StreamReader filereader = new System.IO.StreamReader(@appDirectory + "\\list");
JArray myjarray = JArray.Parse(filereader.ReadToEnd());
filereader.Close();
string json = " {\"token\":\"" + catalog.Token + "\",\"name\":\"" + catalog.Name +"\",\"logo\":\"" + catalog.Logo + "\",\"theme\":\"" + catalog.Theme + "\"}";
JObject myCatalogAsJObject = JObject.Parse(json);
myjarray.Remove(myCatalogAsJObject);
}
I want to remove the JObject corresponding to myCatalogAsJObject
variable, but it doesn't work, because the answer of myjarray.Contains(myCatalogAsJObject)
is false.
The problem is that myjarray
actually contains it : it's the only JObject in my JArray.
If I do myCatalogAsJObject.ToString().Equals(myjarray.First.ToString())
, the answer is true however.
I'm stuck.
.Contains
(and .Remove
) by default will compare references. Since you're creating a new JObject
, the array does not contain that instance.
You could get the instance of the object from the array and remove that:
JObject match = myjarray.FirstOrDefault(j => j.token == catalog.token &&
j.name == catalog.name &&
j.logo == catalog.logo &&
j.theme == catalog.theme);
myjarray.Remove(match);
EDIT : Here is your code, simplified :
JToken match = myjarray.FirstOrDefault(j => j.ToString().Equals(myCatalogAsJObject.ToString()));
myjarray.Remove(match);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With