none
List in a List - For a project called Order System RRS feed

  • Question

  • Hi,

    First of all, I am sorry if this is the wrong forum. 

    I have some troubles with an order system that i am going to create. I have a form where I first want the user to create an order in a Windows form by just  writing "customer name" and a unique "order number". 

    Below this (in the same  Windows form) I want the user to create order rows with information inside (order row, article name, article number, units and price per unit) where each order row number is unique. After creating an order row, i.e order row 1, the user should be able to add more order rows if they like. 

    What this mean is that i want to have a List<> containing "customer name" and "order number" as well as containing a List<> with order rows. I want a list inside a list. 

    Is there anyone out there that knows how to solve this? 

    Tuesday, November 12, 2019 1:35 PM

Answers

  • I recommend you to build more accurate object model than using lists. So instead of having list inside list, there would be Order class that can include instances of OrderRow class. And if you need store multiple orders then you could create for example "OrderList" class for that.

    Just for example something like could be start:

    public class Order
    {
        private List<OrderRow> OrderRows { get; set; } = new List<OrderRow>();
    
        public string CustomerName { get; set; }
    
        public string OrderCode { get; set; }
    
        public void AddOrderRow(OrderRow row)
        {
            OrderRows.Add(row);
        }
    
        public IEnumerable<OrderRow> GetOrderRows()
        {
            foreach (OrderRow row in OrderRows)
                yield return row;
        }
    }
    
    public class OrderRow
    {
        public string ArticleName { get; set; }
    
        public string ArticleNumber { get; set; }
    
        public int Unit { get; set; }
    
        public decimal PricePerUnit { get; set; }
    }
    
    public class OrderList
    {
        private List<Order> Orders { get; set; } = new List<Order>();
    
        public void Add(Order order)
        {
            Orders.Add(order);
        }
    
        public void Remove(Order order)
        {
            Orders.Remove(order);
        }
    
        public bool Contains(Order order)
        {
            return Orders.Contains(order);
        }
    
        public IEnumerable GetOrders()
        {
            foreach (Order order in Orders)
                yield return order;
        }
    }

    Tuesday, November 12, 2019 1:52 PM
  • I have some troubles with an order system that i am going to create. I have a form where I first want the user to create an order in a Windows form by just  writing "customer name" and a unique "order number". 

    The customer assigns an unique order number is something the customer does not do. The program assigns the unique order number for a given order added to the database. The database assigns the order number,  an unique ID number,  for each order record added to the Order table.

    Each OderDetail record is added to the OrderDetail table that has its unique ID assigned to OdderDetail record when added to the OrderDetail table, and it also has the ID of the Order record assigned to the OrderDetail record linking the OrderDetail record to a given Order record, which is called a 1-to-many relationship.

    https://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

    Yes, you can have a list of Orders that can have a list of  OrderDetails for each Order which are collections a collection with in a collection.

    The simple way to do this is to understand how to use the ADO.NET Entity Framework that creates collection of objects and can create a  collection of objects with in a collection as you work with the database in persisting data to the database. 

    The fist link below talks about the importance of using collections and the role EF can play to help you with using collections as EF has that feature.

    https://dzone.com/articles/reasons-move-datatables

    EF is an ORM

    https://www.tutorialspoint.com/entity_framework/entity_framework_overview.htm

    The two links below give the basics of creating an application using EF. The fiest link is in C#, but it doesn't have the pictures of the forms and whatnot.

    https://www.codeguru.com/csharp/csharp/net30/article.php/c15489/ADONET-Entity-Framework-Tutorial-and-Basics.htm

    The second link is in VB that is the same tutorial as the link for C#, but the like has the form pictures and whatnot that are missing in the C# link. It looks like the link doesn't work but it does eventally allow you do start scrolling down to the first page.

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    I think the tutorial and the making of the simple little application using Windows forms and EF will give you everything you need to know on how to make things work. 

    You can use Localdb which is part of MS SQL Server Express to make your application, which is required to do the tutorial too.

    https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/

    The Service Based Database in Visual Stuido is a Localdb database, starting with Visual Stuido 2012 and later versions of Visual Stuido. 

    https://docs.microsoft.com/en-us/visualstudio/data-tools/create-a-sql-database-by-using-a-designer?view=vs-2019

    You should install one of the Visual Stuido Community editions that will have Service Based Database as a feature.

    It's all laid before you,  and all you have to do is follow the path and understand it in helping you make your program. 




    Tuesday, November 12, 2019 2:32 PM

All replies

  • I recommend you to build more accurate object model than using lists. So instead of having list inside list, there would be Order class that can include instances of OrderRow class. And if you need store multiple orders then you could create for example "OrderList" class for that.

    Just for example something like could be start:

    public class Order
    {
        private List<OrderRow> OrderRows { get; set; } = new List<OrderRow>();
    
        public string CustomerName { get; set; }
    
        public string OrderCode { get; set; }
    
        public void AddOrderRow(OrderRow row)
        {
            OrderRows.Add(row);
        }
    
        public IEnumerable<OrderRow> GetOrderRows()
        {
            foreach (OrderRow row in OrderRows)
                yield return row;
        }
    }
    
    public class OrderRow
    {
        public string ArticleName { get; set; }
    
        public string ArticleNumber { get; set; }
    
        public int Unit { get; set; }
    
        public decimal PricePerUnit { get; set; }
    }
    
    public class OrderList
    {
        private List<Order> Orders { get; set; } = new List<Order>();
    
        public void Add(Order order)
        {
            Orders.Add(order);
        }
    
        public void Remove(Order order)
        {
            Orders.Remove(order);
        }
    
        public bool Contains(Order order)
        {
            return Orders.Contains(order);
        }
    
        public IEnumerable GetOrders()
        {
            foreach (Order order in Orders)
                yield return order;
        }
    }

    Tuesday, November 12, 2019 1:52 PM
  • I have some troubles with an order system that i am going to create. I have a form where I first want the user to create an order in a Windows form by just  writing "customer name" and a unique "order number". 

    The customer assigns an unique order number is something the customer does not do. The program assigns the unique order number for a given order added to the database. The database assigns the order number,  an unique ID number,  for each order record added to the Order table.

    Each OderDetail record is added to the OrderDetail table that has its unique ID assigned to OdderDetail record when added to the OrderDetail table, and it also has the ID of the Order record assigned to the OrderDetail record linking the OrderDetail record to a given Order record, which is called a 1-to-many relationship.

    https://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

    Yes, you can have a list of Orders that can have a list of  OrderDetails for each Order which are collections a collection with in a collection.

    The simple way to do this is to understand how to use the ADO.NET Entity Framework that creates collection of objects and can create a  collection of objects with in a collection as you work with the database in persisting data to the database. 

    The fist link below talks about the importance of using collections and the role EF can play to help you with using collections as EF has that feature.

    https://dzone.com/articles/reasons-move-datatables

    EF is an ORM

    https://www.tutorialspoint.com/entity_framework/entity_framework_overview.htm

    The two links below give the basics of creating an application using EF. The fiest link is in C#, but it doesn't have the pictures of the forms and whatnot.

    https://www.codeguru.com/csharp/csharp/net30/article.php/c15489/ADONET-Entity-Framework-Tutorial-and-Basics.htm

    The second link is in VB that is the same tutorial as the link for C#, but the like has the form pictures and whatnot that are missing in the C# link. It looks like the link doesn't work but it does eventally allow you do start scrolling down to the first page.

    http://www.vbforums.com/showthread.php?540421-Tutorial-An-Introduction-to-the-ADO-NET-Entity-Framework

    I think the tutorial and the making of the simple little application using Windows forms and EF will give you everything you need to know on how to make things work. 

    You can use Localdb which is part of MS SQL Server Express to make your application, which is required to do the tutorial too.

    https://blogs.msdn.microsoft.com/sqlexpress/2011/07/12/introducing-localdb-an-improved-sql-express/

    The Service Based Database in Visual Stuido is a Localdb database, starting with Visual Stuido 2012 and later versions of Visual Stuido. 

    https://docs.microsoft.com/en-us/visualstudio/data-tools/create-a-sql-database-by-using-a-designer?view=vs-2019

    You should install one of the Visual Stuido Community editions that will have Service Based Database as a feature.

    It's all laid before you,  and all you have to do is follow the path and understand it in helping you make your program. 




    Tuesday, November 12, 2019 2:32 PM
  • Thank you so much MasaSam! I will try this code in my system
    Tuesday, November 12, 2019 6:46 PM
  • Thank you so much for all the information! I'm glad that you shared all of this
    Tuesday, November 12, 2019 6:47 PM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 9, 2020 9:19 AM