0

I have a class like

 public class CommonMasterDataResponse
{
    public List<MyClass> dto1{ get; set; }
    public List<MyClass> dto2 { get; set; }
    public List<MyClass> dto3 { get; set; }
    .
    .
    .
    public List<MyClass> dto100 { get; set; }
}

I have return it from my web api method. Now i want to remove all properties which has no data. like if in web api I have assigned data to dt01 and dto2 then if I return CommonMasterDataResponse object then it render only dto1 and dto2. I am very new to c#. kinldy give me suggestion how to chieve this.

angfreak
  • 993
  • 2
  • 11
  • 27

1 Answers1

0

Can you try this piece of code?

public class CommonMasterDataResponse
{
    public CommonMasterDataResponse()
    {

    }

    public bool ShouldSerializeDto1()
    {
        return Dto1.Any();
    }

    [XmlElement(IsNullable = true)]
    public List<string> Dto1 { get; set; }
}

But, I think in general this is a bad practice. Try to create separate Dtos and API end points for every DTO. That way you can avoid this situation. It is easy code and maintain. Also, I believe this would be a performance hit in both the server and client.

Priyan Perera
  • 560
  • 1
  • 7
  • 20