0

I have the following list:

var menuList = new List<MenuItem>;

The first element of this List is:

menuList.Add(new List<MenuItem>)

I want to add an element of type MenuItem at the first position of the List which is in the another List.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Svetoslav
  • 99
  • 2
  • 14
  • 1
    So the question is how to merge two `List`s keeping second `List` first ? – Hari Prasad Mar 28 '16 at 07:53
  • 3
    Your question is somewhat unclear as a description - could you give a [mcve] which shows what you mean? (In particular, one element of a `List` can't be another `List` - it has to be a `MenuItem`.) – Jon Skeet Mar 28 '16 at 07:55
  • No . I simply want to add an element at first position to a list which is in another list. Like list.Insert(0 , MenuItem) but if you want to do this in list in list .NET doesn't give me this option. – Svetoslav Mar 28 '16 at 07:56
  • 1
    You can see at answer here: http://stackoverflow.com/questions/4745994/how-can-i-add-to-a-lists-first-position – vppuzakov Mar 28 '16 at 08:06
  • 1
    Possible duplicate of [How to add item to the beginning of List?](http://stackoverflow.com/questions/390491/how-to-add-item-to-the-beginning-of-listt) – Aliasghar Bahrami Mar 28 '16 at 08:26

3 Answers3

1

Reference : https://msdn.microsoft.com/en-us/library/sey5k5z4.aspx

public void Insert(
    int index,
    T item
)
Marco Bong
  • 690
  • 4
  • 13
1

you are not permitted to Add/insert List<MenuItem> To menuList since it is defined as List<MenuItem> so that which will accept only MenuItem to form the required list. Don't worry you can achieve this by using List<List<MenuItem>> so that you can add/insert List<MenuItem>s to the SuperMenu

List<MenuItem> menuList = new List<MenuItem>();
menuList.Add(new MenuItem() { Name = "a",..  });
menuList.Insert(0, new MenuItem() { Name = "B",.. });
List<List<MenuItem>> SuperMenu = new List<List<MenuItem>>();
SuperMenu.Add(menuList);
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

This is the code for a list of lists...in other words a list having its items as lists

List<List<MenuItem>> menuItems = new List<List<MenuItem>>();
for (int i = 0 ; i < menuItems.Count();  i++)
{
   MenuItem itemToInsert; // "something";
   List<MenuItem> innerList = menuItems[1];//Returns inner list at index i
   innerList.Insert(0, itemToInsert);// Now insert anywhere you want
}
Ashraf Ali
  • 573
  • 4
  • 9