locked
C# populate nested list type class RRS feed

  • Question

  • User264732274 posted

    due to lack of command over coding i am not being able to populate class in code.

    here is my class hierarchy

    public class MainViewModel
        {
            public List<Student> Students { get; set; }
        }
    
        public class Student
        {
            int ID = 0;
            string Name = "";
            List<States> States { get; set; }
        }
    
        public class States
        {
            int ID = 0;
            string Name = "";
            List<Cities> Cities { get; set; }
        }
    
        public class Cities
        {
            int ID = 0;
            string Name = "";
        }

    i want to populate below fashion but stuck.

        MainViewModel oVm = new MainViewModel()
        {
            Students = new List<Student>() {
                new Student
                {
                    ID="Sukumar",
                    Name="pop",
                };
            }
    
        };

    i want populate in nested way....the way i am trying to do so. need bit help for syntax. thanks

    Sunday, January 10, 2016 6:46 PM

Answers

  • User303363814 posted
    MainViewModel oVm = new MainViewModel()
        {
            Students = new List<Student>() {
                new Student
                {
                    ID="Sukumar",
                    Name="pop",
                    Cities = new List<Cities>() {
                        new Cities { ID = 0, Name="Alpha" },
                        new Cities { ID = 1, Name="Beta" }
                    }
                };
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 10, 2016 10:34 PM
  • User-986267747 posted

    Hi sudip_inn,

    i want populate in nested way....the way i am trying to do so. need bit help for syntax. thanks

    int ID = 0; string Name = "";

    According to your code, the field called ID and the filed called Name is private and have the default value, so we couldn't assign values to these field. I suspect that you'd like to learn how to populate some values in nested way. You could refer to the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication3
    {
        public class MainViewModel
        {
            public List<Student> Students { get; set; }
        }
    
        public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<State> States { get; set; }
        }
    
        public class State
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<City> Cities { get; set; }
        }
    
        public class City
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                MainViewModel oVm = new MainViewModel()
                                    {
                                        Students = new List<Student> {
                                            new Student
                                            {
                                                ID=1,
                                                Name="Sup",
                                                States=new List<State> { new State { ID=11, Name="sate11", Cities= new List<City> { new City { ID=111, Name="city111"}}}, 
                                                                         new State { ID=12, Name="sate12", Cities= new List<City> { new City { ID=121, Name="city121"}}}
                                                                       }
                                            },
                                             new Student
                                            {
                                                ID=2,
                                                Name="Kle",
                                                 States=new List<State> { new State { ID=21, Name="sate21", Cities= new List<City> { new City { ID=111, Name="city111"}}}, 
                                                                         new State { ID=22, Name="sate22", Cities= new List<City> { new City { ID=111, Name="city121"}}}
                                                                       }
                                            }
                                        }
    
                                    };
                Console.ReadKey();
            }
        }
    }
    

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2016 5:48 AM

All replies

  • User303363814 posted
    MainViewModel oVm = new MainViewModel()
        {
            Students = new List<Student>() {
                new Student
                {
                    ID="Sukumar",
                    Name="pop",
                    Cities = new List<Cities>() {
                        new Cities { ID = 0, Name="Alpha" },
                        new Cities { ID = 1, Name="Beta" }
                    }
                };
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 10, 2016 10:34 PM
  • User-986267747 posted

    Hi sudip_inn,

    i want populate in nested way....the way i am trying to do so. need bit help for syntax. thanks

    int ID = 0; string Name = "";

    According to your code, the field called ID and the filed called Name is private and have the default value, so we couldn't assign values to these field. I suspect that you'd like to learn how to populate some values in nested way. You could refer to the following code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication3
    {
        public class MainViewModel
        {
            public List<Student> Students { get; set; }
        }
    
        public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<State> States { get; set; }
        }
    
        public class State
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<City> Cities { get; set; }
        }
    
        public class City
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
    
                MainViewModel oVm = new MainViewModel()
                                    {
                                        Students = new List<Student> {
                                            new Student
                                            {
                                                ID=1,
                                                Name="Sup",
                                                States=new List<State> { new State { ID=11, Name="sate11", Cities= new List<City> { new City { ID=111, Name="city111"}}}, 
                                                                         new State { ID=12, Name="sate12", Cities= new List<City> { new City { ID=121, Name="city121"}}}
                                                                       }
                                            },
                                             new Student
                                            {
                                                ID=2,
                                                Name="Kle",
                                                 States=new List<State> { new State { ID=21, Name="sate21", Cities= new List<City> { new City { ID=111, Name="city111"}}}, 
                                                                         new State { ID=22, Name="sate22", Cities= new List<City> { new City { ID=111, Name="city121"}}}
                                                                       }
                                            }
                                        }
    
                                    };
                Console.ReadKey();
            }
        }
    }
    

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 11, 2016 5:48 AM