如何删除null元素表单List

时间:2018-02-14 11:00:32

标签: c# asp.net

嗨,我是List处理的新手。请帮我如何从列表中删除null元素。

让我用一个例子来解释我的问题。以下是我所拥有的两个班级。

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;}
}

以下数据我绑定到Student类。结果我们获得了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},
}

这里我想删除字段,如果它包含空值并以json格式发送这些数据。

最终所需格式如下。

{
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"},
}

请注意,不应该有任何字段为空值。

0 个答案:

没有答案