3.0/LINQ Menu Structures-VBForums
Results 1 to 8 of 8

Thread: Menu Structures

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Menu Structures

    Hi There,

    i have this structure:
    this is populated from a database.

    Name:  menu.jpg
Views: 59
Size:  23.6 KB

    this is how it looks like
    Name:  Menu look.jpg
Views: 59
Size:  6.6 KB

    i need a little help on how to design a function that can achieve this structure in code.im just thinking of using a recursive function.but i dont know how to start..

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Menu Structures

    what technology are you using? i.e. bulk standard C#. mvc etc.

    In MVC, if you were to use lazy loading on the context from the top node going via Entity Framework for instance, then this will load this kind of structure into the objects for you. (could be slow though if you have loads of data).

    Otherwise if your using linq to SQL then you can do something like:

    http://stackoverflow.com/questions/1...-relationships

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Menu Structures

    hi bill,

    i am using windows application c#.my problems is how to put the idea on a function.

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Menu Structures

    so you have something like

    public struct mystruct {.......}

    and presumably you have some kind of collection holding the structs.

    lets say your collection is called mycollection.

    ForEach (mystruct inrec in mycollection)
    {

    here you use a treecontrol and use the parentid with id to determine weather to create a node or childnode. The problem you have here though is that you have 2 root nodes.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Menu Structures

    Hi billy,

    Code:
    private void CreateMenu()
    {
       // returns menu structures above
       var menu = menulogic.getMenu(role);
    
       foreach (var m in menu.orderby(x=>x.menuindex))
       {
          //I dont know what next in here.
       }
    }

  6. #6
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Menu Structures

    You should look into recursive functions.
    I haven't worked with LINQ yet, but it should be fairly easy to first select the items where the parent is 0, and then recursively call the function again and then use the parent's item as index.

    Something like:
    Code:
    private void CreateMenu(var menu, int parent = 0)
    {
        foreach(var m in menu.Where(x=>x.ParentID == parent))
        {
            // output m value
            // ...
    
            // Recurse
            CreateMenu(menu, m.ID);
        }
    }
    Delete it. They just clutter threads anyway.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: Menu Structures

    Hi BigB,

    i cannot use var datatype as parameter and also a default value.i am using vs2008 and .netframework3.5.

  8. #8
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Menu Structures

    var menu = menulogic.getMenu(role);

    You have stated that this returns a menu struct.

    What does menulogic.getMenu(role) actually return?

    is it a structure, a collection of structure items etc.

    eg.

    if getMenu method is declared as:

    public myStruct getMenu(int role) {}

    for instance, then do not return it into a var , but into a myStruct
    myStruct menu = menulogic.getMenu(role);

    Now you can pass menu since from BIgB

    private void CreateMenu(var menu, int parent = 0)

    now becomes

    private void CreateMenu(myStruct menu, int parent = 0)

    you might have to change your structure to a class.
    Thus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.