0

Hi I am new to List handling. Please help me how to remove the null elements from the List.

Let me explain my question with an example. The below are the two class which I have.

public class Student
{
    public int id{get;set;}
    public string Name{get;set;}
    public Book book{get;set;}
}

public class Book
{
    public String BookName{get;set;}
    public int BookId{get;set;}
}

The below data i am binding to Student class. Which in result we get the List.

{
Student: {"id":1,"Name":"John","book":{"BookName":"Math","BookId":1}},
Student: {"id":3,"Name":"John","book":{"BookName":"Math","BookId":2}},
Student: {"id":4,"Name":"John","book":null},
}

Here I want to remove the fields, if it contains null value and send these data in json format.

Ultimately the required format is as below.

{
Student: {"id":1,"Name":"John","book":{"BookName":"Math","BookId":1}},
Student: {"id":3,"Name":"John","book":{"BookName":"Math","BookId":2}},
Student: {"id":4,"Name":"John"},
}

Please note that there should not any fields with null value.

FaizanHussainRabbani
  • 3,256
  • 3
  • 26
  • 46
Charan Kumar
  • 45
  • 1
  • 6

0 Answers0