How do I add a list to a list?

Ok I have this:

if(something!=null)
{
  SubMenu.Add(new SubMenuModel("PERSONAL_INFORMATION","account.personalinformation","/account/personalinformation"));
}
  if(something123!=null)
{        
   SubMenu.Add(new SubMenuModel("NOTIFICATIONS", "account.notificationsettings", "/account/notifications"));
}
      SubMenu.Add(new SubMenuModel("CHANGE_PASSWORD", "account.changepassword", "/account/passwordchange"));
      SubMenu.Add(new SubMenuModel("GAME_SETTINGS", "default", "default"));

    MainMenu.Add(new MainMenuModel("SETTINGS", "default", "default", true,




           }));

      

EDIT:

public List<SubMenuModel> SubMenu { get; set; } 
public List<MainMenuModel> MainMenu { get; set; }

      

How can I add this SubMenu

to Main menu

because I have a condition, so if the condition is! = Null add to the list ... any sugesstion?

+3


source to share


2 answers


you can follow this link.

you only need to do MainMenu.AddRange(SubMenu);

Hope I can help you.

EDIT: Ok, so you can do this. I think you can add MenuItems to an existing MenuItem like:



MenuItem addDevice = new MenuItem("Add Device");
addDevice.MenuItems.Add( new MenuItem("Add More .."));
It would be visible like:

      

submenu

I see this solution here and it works for me.

+4


source


If your MainMenu and SubMenu share the same property or method, then I suggest you use an interface.

    public interface IMenu
    {
        int a { get; set; }
        string b {get; set; }
    }

    public class SubMenu : IMenu
    {
        public int a { get; set; }

        public string b { get; set; }

        public double c { get; set; }
    }

    public class MainMenu : IMenu
    {
        public int a { get; set; }

        public string b { get; set; }

        public string d { get; set; }
    }

      

So you can use addrange like this

        List<IMenu> menuList = new List<IMenu>();
        List<MainMenu> mainMenuList = new List<MainMenu>();
        List<SubMenu> subMenuList = new List<SubMenu>();

        menuList .AddRange(mainMenuList);
        menuList .AddRange(subMenuList);

      



Edit ::

Suggest on this sentence "@ Boot750 I have this in the main menu public ListSubMenu {get; set;}"

So just do it like this

var mainMenu = new List<MainMenu>();
var subMenuToAdd = new List<SubMenu>().Add(new SubMenu() { .. });

mainMenu.SubMenu = subMenuToAdd;

      

0


source







All Articles