none
How to get Last Order Date for a specific customer

    Question

  • Hi Everybody

    I have an order management application where two entities are there. i.e- Customer & Order. The relationship between these two is:

    a customer can have many order

    an order must have a customer.

    My Requirement is:

    I want to retrieve last order date for a customer from the collection of order instances for that customer.

    Any help will be appreciated. (Please provide C# code).


    Rashmi Ranjan Panigrahi

    Wednesday, May 23, 2012 12:08 PM

Answers

  • For a computed field on Customer you'd do it in much the same way

    Order LastOrder = this.Orders.OrderBy(OrderDate).LastOrDefault;
    if (LastOrder != null) {
    	result = LastOrder.Date;
    } else {
    	result = null;
    }

    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Marked as answer by babloo1436 Wednesday, May 23, 2012 2:53 PM
    Wednesday, May 23, 2012 2:32 PM

All replies

  • I'm assuming you have a Query that Filters your Orders based on the Customer. Add a Sort Ascending on the Date.

    Then:

    DisplayProperty = this.QueryName.Last()

    Wednesday, May 23, 2012 2:06 PM
  • I'd use LastOrDefault to avoid causing an error if there aren't any orders for a customer - returns null instead which you can then check for.

    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    Wednesday, May 23, 2012 2:25 PM
  • Hi Kyle

    I am afraid, i don't have any query to filter orders based on customer. Its everything depending upon relationship as i mentioned above.

    I think i can achieve this in a computed date property in customer entity. But i don't know how. 

    If you have any idea on this, then please suggest me.

    Thanks


    Rashmi Ranjan Panigrahi

    Wednesday, May 23, 2012 2:25 PM
  • For a computed field on Customer you'd do it in much the same way

    Order LastOrder = this.Orders.OrderBy(OrderDate).LastOrDefault;
    if (LastOrder != null) {
    	result = LastOrder.Date;
    } else {
    	result = null;
    }

    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Marked as answer by babloo1436 Wednesday, May 23, 2012 2:53 PM
    Wednesday, May 23, 2012 2:32 PM
  • Good point @Simon Jones. I have no idea why I wrote 'Last'. Time for more coffee.
    Wednesday, May 23, 2012 2:34 PM