izengod
izengod

Reputation: 1156

how to convert IEnumerable<JToken> to JArray

I'm using LINQ over a JArray to filter out the items based on a particular condition and want that result in a separate JArray.

JArray arrSameClass = (JArray) arrPupilEmailDetails.Where(joSameClass => joSameClass["uClassId"].ToString() == gidClassId.ToString());

But this is giving me an casting exception('unable to cast from IEnumerable<JToken> to JArray'). I've tried JArray.Parse() also. Any help ?

Upvotes: 15

Views: 21924

Answers (1)

dbc
dbc

Reputation: 116605

You can use the JArray(Object) constructor and pass it your IEnumerable<JToken> and the enumerable will be evaluated and used to construct the JArray:

var query = arrPupilEmailDetails.Where(joSameClass => joSameClass["uClassId"].ToString() == gidClassId.ToString());
var arrSameClass = new JArray(query);

Sample fiddle.

Upvotes: 21

Related Questions