Newtonsoft.Json.Linq.JObject
implemented IEnumerable<T>
, and not explicit implementation, but why can't do this:
using System.Linq;
...
var jobj = new JObject();
var xxx = jobj.Select(x => x); //error
foreach(var x in jobj) { } //no error
WHY? Thanks.
JObject
implements both IEnumerable<KeyValuePair<string, JToken>>
and IEnumerable<JToken>
(by inheriting from JContainer
).
Thus you cannot use LINQ (e.g. Select
) directly since it doesn't know which of the enumerables to 'extend'.
Thus you need to cast first:
((IEnumerable<KeyValuePair<string, JToken>>) jobj).Select(x => x)
or:
jobj.Cast<KeyValuePair<string, JToken>>().Select(x => x)
or as @Evk pointed out:
jobj.Select((KeyValuePair<string, JToken> x) => x)
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