Populating Nested List<> in MVC4 C#

707 views Asked by At

I've got a problem populating nested List<>

The object graph looks like this:

Route ⇒ Section ⇒ Co-ordinates

Whenever I try to populate Сoordinates list it just overwrites previous record and at the end gives me only the last Coordinate record. But I want all the Co-ordinates.

Here is my controller code:

List<RequestRouteDataClass> result = new List<RequestRouteDataClass> {  
     new RequestRouteDataClass() { 
         RouteRequestId = objRouteManagement.RouteRequestId, 
         RouteName = objRouteManagement.RouteName, 
         RouteDescription = objRouteManagement.RouteDescription, 
         RouteSections = new List<RouteSections> { 
         new RouteSections() { 
             Route_Sections_Id = objSections.Route_Sections_Id, 
             Section_Speed = objSections.Section_Speed, 
             Section_Description = objSections.Section_Description,                                                     
             RouteCordinatesSections = new List<SectionCoordinatesRelationData> { 
                 new SectionCoordinatesRelationData() { 
                     SectionCoordinate_Relat_Id = objSectionsCordinates.SectionCoordinate_Relat_Id, 
                     CoordinateLat = objSectionsCordinates.CoordinateLat, 
                     CoordinateLag = objSectionsCordinates.CoordinateLag  
                 }
             }
         }
     } 
 }
2

There are 2 answers

1
cracker On BEST ANSWER

If you want to use Nested List.

Your Model Contains =>

public class MainModelToUse
    {
        public MainModelToUse()
        {
            FirstListObject = new List<FirstListClass>();
        }

        public List<FirstListClass> FirstListObject { get; set; }
    }


   public class FirstListClass
    {
        public FirstListClass()
        {
            SecondListObject = new List<SecondListClass>();
        }

        public List<SecondListClass> SecondListObject { get; set; }
    }


  public class SecondListClass
     {
        public SecondListClass()
        {
            ThirdListObject = new List<ThirdListClass>();
        }

        public List<ThirdListClass> ThirdListObject { get; set; }
    }


   public class ThirdListClass
    {

    }

Your Code to Nested List =>

        FirstListClass vmFirstClassMenu = new FirstListClass();
            vmFirstClassMenu.SecondListClass = new List<SecondListClass>();
            FirstListClass vmFirstClassCategory = new FirstListClass();


            var dataObject1 = //Get Data By Query In Object;

            foreach (Model objModel in dataObject1)
            {
                vmFirstClassCategory = new FirstListClass
                {
                    //Your Items
                };

                var DataObject2 = //Get Data By Query In Object;

                vmFirstClassCategory.SecondListClass = new List<SecondListClass>();

                foreach (SecondListClass menuItem in DataObject2)
                {
                    SecondListClass vmFirstClassMenuItem = new SecondListClass
                    {
                       //Your Items
                    };

                    var DataObject3 = //Get Data By Query In Object;
                    vmFirstClassMenuItem.ThirdListClass = new List<ThirdListClass>();

                    foreach (ThirdListClass price in DataObject3)
                    {
                        ThirdListClass vmThirdClassobj = new ThirdListClass
                        {
                            //Your Items
                        };

                        vmFirstClassMenuItem.ThirdListClass.Add(vmThirdClassobj);
                    }
                    vmFirstClassCategory.SecondListClass.Add(vmFirstClassMenuItem);
                }                
            }   

Hope this is what you are looking for.

0
Sam On

First off: spacing helps with readability (edit: but I see you fixed that in your question already):

 List<RequestRouteDataClass> result = new List<RequestRouteDataClass> 
 { 
     new RequestRouteDataClass() 
     {
         RouteRequestId = objRouteManagement.RouteRequestId, 
         RouteName = objRouteManagement.RouteName, 
         RouteDescription = objRouteManagement.RouteDescription, 
         RouteSections = new List<RouteSections> 
         { 
             new RouteSections()  
             { 
                 Route_Sections_Id = objSections.Route_Sections_Id, 
                 Section_Speed = objSections.Section_Speed, 
                 Section_Description = objSections.Section_Description,                                                     
                 RouteCordinatesSections = new List<SectionCoordinatesRelationData> 
                 { 
                     new SectionCoordinatesRelationData() 
                     { 
                         SectionCoordinate_Relat_Id = objSectionsCordinates.SectionCoordinate_Relat_Id, 
                         CoordinateLat = objSectionsCordinates.CoordinateLat, 
                         CoordinateLag =objSectionsCordinates.CoordinateLag 
                     } 
                 } 
             }
         } 
     }
 };

Next: what you are doing with the above is initiating your lists with a single element in each list. If you want more elements, you have to add them. I recommend using a foreach and the Add() functionality to fill your lists.

From your example it is not clear how your source data is stored, but if you have multiples of something I would expect those too to be in a list or an array of some kind.