locked
Recursive method RRS feed

  • Question

  • class Foo
    {
       List<Foo> SubFoos
    
       List<Order> Orders
    }
    
    class Order
    {
      Guid parentFooId
    }
    
    I need to write a recursive function to get a particular order object from a collection of Foo objects given the orderId and fooId.

    Anonymous


    • Edited by DevCoder Tuesday, October 22, 2013 9:20 PM
    Tuesday, October 22, 2013 8:50 PM

Answers

  • Add a method to Foo called GetOrder

    pubic Order GetOrder(Guid fooId, Guid OrderId)
    {
       if (Id == fooId)
       {
          // This is the right Foo
          // return the Order or null if not found
          return Orders.FirstOrDefault(o => o.Id == OrderId);
    
       // This is not the right Foo
       // ask each of the subFoos
       foreach(var foo in SubFoos)
       {
          if ((var result = foo.GetOrder(fooId, orderId) != null)
          {
             // This is the one
             return result;
          }
       }
    
       // I am not the Foo/Order and neither are any of my subs
       return null;
    }

    (Written, not tested - double your money back if not correct!)

    Paul Linton

    • Marked as answer by DevCoder Wednesday, October 23, 2013 3:37 AM
    Wednesday, October 23, 2013 3:09 AM

All replies

  • Since you have a list of Foo objects and Order objects. you can use OrderBy method of List class, 

    List<Order> SortedOrderList = orderList.OrderBy(o=>o.orderID).ToList();

    List<Foo> SortedFooList = fooList.OrderBy(f=>f.orderID).ToList();

    Where orderList, fooList are the original populated lists and SortedOrderList, SortedFooList are the sorted lists.


    Regards, Pa445


    • Edited by Pa_445 Tuesday, October 22, 2013 9:07 PM
    Tuesday, October 22, 2013 9:05 PM
  • Sorry, i probably didn't explain well. I need to retrieve the Order object from a collection of Foo given the order id. Order is the name of the object, it is not a sort order.

    Anonymous


    • Edited by DevCoder Tuesday, October 22, 2013 9:23 PM
    Tuesday, October 22, 2013 9:22 PM
  • Ok, then use this

    Order order = fooList.Find(
                delegate(Order o)
                {
                    return o.orderID == "<The Id to compare to>";
                }
                );


    Regards, Pa445

    Tuesday, October 22, 2013 9:45 PM
  • But Foo also has a list of Foo objects. The Order object we are looking for could be nested many levels deep. Please take a look at my object model.

    Anonymous

    Tuesday, October 22, 2013 10:11 PM
  • Recursion always gives me a headache, but I think this is what you're looking for. I'm assuming that the Order class has a Guid called ID.
    Order GetOrder(Guid orderID)
    {
       foreach(Foo foo in SubFoo)
       {
          Order order = foo.GetOrder(orderID); // This is the recursive bit.
          if(order != null)
             return order;
       } 
    
       foreach(Order order in Orders)
       {
          if(order.ID == orderID)
             return order;
       }
    
       return null;
    }
    Tuesday, October 22, 2013 10:48 PM
  • Actually, I've just realised that what I wrote is technically not recursion, because the method never calls itself, but rather calls the same method on other objects. Nevertheless it should work (I think).
    Tuesday, October 22, 2013 10:56 PM
  • Does Foo have an id property?

    Does Order have an id property?

    Are the classes more like?

    public class Foo {
       public Guid Id {get;set;}
       public List<Foo> SubFoos {get;set;}
       public List<Order> Orders {get; set;}
    }
    
    public class Order {
       public Guid Id {get;set;}
    }
    
    And is your request that you want to find a Foo whose Id is equal to a given value and then return the Order within that Foo that has a given Id?

    Paul Linton

    Tuesday, October 22, 2013 11:43 PM
  • Yes that is correct.

    Anonymous

    Wednesday, October 23, 2013 2:57 AM
  • Add a method to Foo called GetOrder

    pubic Order GetOrder(Guid fooId, Guid OrderId)
    {
       if (Id == fooId)
       {
          // This is the right Foo
          // return the Order or null if not found
          return Orders.FirstOrDefault(o => o.Id == OrderId);
    
       // This is not the right Foo
       // ask each of the subFoos
       foreach(var foo in SubFoos)
       {
          if ((var result = foo.GetOrder(fooId, orderId) != null)
          {
             // This is the one
             return result;
          }
       }
    
       // I am not the Foo/Order and neither are any of my subs
       return null;
    }

    (Written, not tested - double your money back if not correct!)

    Paul Linton

    • Marked as answer by DevCoder Wednesday, October 23, 2013 3:37 AM
    Wednesday, October 23, 2013 3:09 AM
  • That worked like magic Paul. Very slick!

    Anonymous

    Wednesday, October 23, 2013 3:37 AM