How to get list of objects from list of jobjects based on key exists or not?

Viewed 3938

This is my code:-

List<JObject> students =[{"id":"101","name":"one","parent_id":"1"},{"id":"102","name":"two","parent_id":"2"},{"id":"103","name":"three"},{"id":"104","name":"four"}];

I tried the following code using Linq but not working

List<JObject> newStudents = students.Where(x => x.Property("parent_id").ToString() == null).ToList();


List<JObject> existedStudents = students.Where(x => x.Property("parent_id").ToString() != null).ToList();

In the above list contains 4 objects , first two objects contains parent_id key next two objects doesn't contain. How to parent_id key existed and not existed list in c#.

3 Answers
Related