Serializing Objects to Xml

Ronald Rex 1,666 Reputation points
2023-11-17T15:18:25.97+00:00

Hey Friends. The code below shows how I am building the results of my queries to be serialized for an xml file. The problem I ran into with my code is what if my results doesnt have any child records where I need to use a foreach loop but I still need to call a method to continue to build my results that will be serialized. I would like to retain the current structure of my code but be able to call a method from a method that doesnt return any child records. As you can see in the last method LoadOrderDetail it doesnt call a method because I am finished with getting the results but lets just say if it needed to call a method and LoadOrderDetails didnt have any child records to iterate over in a foreach loop.

 public static List<Customer> LoadCustomers()
 {
     using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
     {
         var customers = cnn.Query<Customer>("select * from Customer", new DynamicParameters());
         List<Customer> result = customers.ToList();

         foreach (var customer in result)
         {
             List<Order> orders = LoadCustomerOrders(customer.Id);
              customer.Orders= orders;   
             
         }
         return result;
     }



       
 }
 public static List<Order> LoadCustomerOrders(int customerId)
 {
     using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
     {
         var orders = cnn.Query<Order>("select * from Orders where Orders.CustomerID=" +  customerId, new DynamicParameters());
         List<Order> result = orders.ToList();
         foreach (var order in result)
         {
             List<OrderDetails> orderDetails = LoadOrderDetails(order.Id);
             order.OrderDetails = orderDetails;
         }

         return result;
     }
 }

 public static List<OrderDetails> LoadOrderDetails(int id)
 {
     using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
     {
         var orders = cnn.Query<OrderDetails>("select * from OrderDetails where OrderDetails.OrderId=" + id, new DynamicParameters());
         List<OrderDetails> result = orders.ToList();

         return result;
     }

 }

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,906 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 44,931 Reputation points Microsoft Vendor
    2023-11-20T05:05:48.49+00:00

    Hi @Ronald Rex , Welcome to Microsoft Q&A,

    You can add some logic inside the method to check if the child record exists. If there are no child records, the related loops and method calls are not executed.

    public static List<Customer> LoadCustomers()
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
        {
            var customers = cnn.Query<Customer>("select * from Customer", new DynamicParameters());
            List<Customer> result = customers.ToList();
    
            foreach (var customer in result)
            {
                List<Order> orders = LoadCustomerOrders(customer.Id);
                
                // Check if there are orders before assigning them to customer
                if (orders.Any())
                {
                    customer.Orders = orders;
    
                    foreach (var order in orders)
                    {
                        List<OrderDetails> orderDetails = LoadOrderDetails(order.Id);
                        
                        // Check if there are order details before assigning them to order
                        if (orderDetails.Any())
                        {
                            order.OrderDetails = orderDetails;
                        }
                    }
                }
            }
    
            return result;
        }
    }
    
    public static List<Order> LoadCustomerOrders(int customerId)
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
        {
            var orders = cnn.Query<Order>("select * from Orders where Orders.CustomerID=" +  customerId, new DynamicParameters());
            List<Order> result = orders.ToList();
            return result;
        }
    }
    
    public static List<OrderDetails> LoadOrderDetails(int id)
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
        {
            var orders = cnn.Query<OrderDetails>("select * from OrderDetails where OrderDetails.OrderId=" + id, new DynamicParameters());
            List<OrderDetails> result = orders.ToList();
            return result;
        }
    }
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.