locked
Exclude null value properties from a class object RRS feed

  • Question

  • User-2103146910 posted

    Dear all,

    In my current project, i have classes with more than 100 properties. i am using WCF service to retrieve data and view it as required in the application. During execution of the application, there are different scenarios and with each scenario i need different properties of a class. sometime 3, sometime 8, sometime 15 properties etc. As the class is very big, i want to return only those properties that don't have null value or properties have values other than null value should return from wcf service [to reduce load and avoid unnecessary data]. so finally i need only the 3, 8, 15 properties from wcf service method call.

    the wcf service is using wshttpbinding and i am working on mvc web application where call to the wcf is done using ajax whenever required. this requirement is specially for this ajax call response data that vary on different scenarios.

    please tell how i can do this more efficiently. please share code snippet to help me.

    Many Thanks

    Sunday, December 16, 2018 6:12 AM

All replies

  • User475983607 posted

    Dear all,

    In my current project, i have classes with more than 100 properties. i am using WCF service to retrieve data and view it as required in the application. During execution of the application, there are different scenarios and with each scenario i need different properties of a class. sometime 3, sometime 8, sometime 15 properties etc. As the class is very big, i want to return only those properties that don't have null value or properties have values other than null value should return from wcf service [to reduce load and avoid unnecessary data]. so finally i need only the 3, 8, 15 properties from wcf service method call.

    the wcf service is using wshttpbinding and i am working on mvc web application where call to the wcf is done using ajax whenever required. this requirement is specially for this ajax call response data that vary on different scenarios.

    please tell how i can do this more efficiently. please share code snippet to help me.

    Many Thanks

    Your describing the default behavior of WCF. Can you provide sample code that reproduces the issue?

    Sunday, December 16, 2018 11:12 PM
  • User-2103146910 posted

    Dear mgebhard,

    i call a service method having wshttpbinding as

    public object FillFields(int _id)
     {
     Travel m = from x in Travel
    where x.id == _id  select x; Travel retObj = new Travel();  retObj.CntryCode = m.CntryCode;  retObj.PrntCntryCode = m.PrntCntryCode;  retObj.CurrCode = m.CurrCode; return retObj; }

    Now Travel class contain more than 100 properties but i need only above three in my current ajax call. similarly there are many other such
    scenarios where group of other properties are required from Travel class.

    Provided that i dont want to create another small class that may contain required properties from this above travel class
    like a class having
    CntryCode
    PrntCntryCode
    CurrCode
    properties only and return this class to the client of wcf service
    secondly i found a solution as below

    (1)

    JsonConvert.SerializeObject(Myobj, Formatting.Indented,
    new JsonSerializerSettings
    {
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    NullValueHandling = NullValueHandling.Ignore
    });
    this way i am getting only required properties/fields from my giant class but
    i need to ask should i return the above generated json string from wcf having
    wshttpbinding. is it feasible or applicable in practice with wshttpbinding and
    not with webhttpbinding(as this binding i particularly use for sending json data
    back to client)

    (2)
    or otherwise i have to serialize the class to a class having non null values using above
    code then deserialize the resulted class to object and send/return back to client
    where it will again serialze to return to my ajax call
    the above scenario is quite repititive.

    that's why i want a clean and easy solution from c#, using reflect or lambda expression
    etc.


    please tell me a c# solution or suggest me if i can use above #1 or #2 solution and it
    is precise or not.

    thanks again

    Monday, December 17, 2018 7:36 AM
  • User-2103146910 posted


    Just to add more i can't use linq as

    var query = from x in Travel
    select new {x.CntryCode,x.PrntCntryCode};

    as i usualty need some values like

    Md retObj = new Md();
    retObj.CntryCode = m.CntryCode + " - " + GetCntryName(m.CntryCode);
    retObj.PrntCntryCode = m.PrntCntryCode + " - " + GetCntryName(m.PrntCntryCode);
    retObj.CurrCode = m.CurrCode + " - " + GetCurrName(m.CurrCode);

    having string type properties

    thanks

    Monday, December 17, 2018 8:36 AM
  • User475983607 posted

    Inputs and outputs are known types in programming.  This is especially true for WCF where all actions and types are interfaces.

    Your design is none standard therefore you are giving up all the goodness baked into the .NET framework.  The server code must return a generic type and the client must know what type to expect or the client must evaluate the return type.  This is code that you must design and write.  Keep in mind that it is difficult to provide assistance with a completely custom design like this.

    Monday, December 17, 2018 12:49 PM