2
public class Order
{
    public decimal Id { get; set; }
    public decimal customerId { get; set; }
    public string payment_method { get; set; }
    public string payment_status { get; set; }
    public double total_price { get; set; }
    public string date { get; set; }
    public List<OrderDetail> OrderDetails { get; set; }
    public List<Address> OrderAddresses { get; set; }
}

public class OrderDetail
{
    public decimal product_id { get; set; }
    public string product_name { get; set; }
    public double price { get; set; }
    public double quantity { get; set; }
}

public class Address
{
    public string type { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string state_province { get; set; }
    public string postal_code { get; set; }
    public string country { get; set; }
    public string telephone { get; set; }
    public string fax { get; set; }
}

here is my model some of my API. using whole model like orderAdd, orderDetail. But, for order listing I want few fields from Order Model e.g Id, total_price, date and exclude rest of the fields and list in Web API response.

below is my response code

[ResponseType(typeof(List<Order>))]
[HttpGet, Route("api/Orders/getOrderList")]
public IHttpActionResult getOrderLists(decimal customerId, int limit = 3)
{
    List<Order> Model = new List<Order>();
    var orders = db.OrderMasters.Where(p => p.CustomerId == customerId).Take(limit);
    foreach (var o in orders)
    {
        Order O = new Order();
        O.Id = o.OrderId;
        O.date = o.PurchasedOn;
        O.total_price = o.Total;
        Model.Add(O);
    }

    return Ok(Model);
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
  • please add code that how you post response back? – Pranav Patel Jan 24 '17 at 09:55
  • One standard practice would be to create an object specifically for the purpose, similar to the viewmodel concept in MVC. Maybe it could be called OrderSummary or something – ADyson Jan 24 '17 at 10:05
  • 2
    Why not just construct a collection of anonymous objects and return as a JSON array, then you aren't constrained by a model....unless you are strongly binding a view against this model somewhere. Alternatively @ADyson has a valid suggestion too – Jak Hammond Jan 24 '17 at 10:06
  • If i don't want to create one more model than? is there any other way? – Muhammad Anas Jan 24 '17 at 10:10
  • @ADyson If i want to develop more API's with excluding some fields than for every API i have to create a new class? – Muhammad Anas Jan 24 '17 at 10:17
  • 1
    http://stackoverflow.com/questions/14486667/suppress-properties-with-null-value-on-asp-net-web-api – Taras Kovalenko Jan 24 '17 at 10:20
  • 1
    @MuhammadAnas well it depends on the specific requirements. You can share classes between API methods if they can return the same structure. Also ask yourself why you need to exclude these fields, or whether it's sufficient for them to be null. Jak Hammond's suggestion of using anonymous objects is also a good one, as you're less tied to a class structure, so it's easier to change, and maybe easier to dynamically configure – ADyson Jan 24 '17 at 10:25

0 Answers0