Loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    Loop

    Hello,

    I created a Linq query which returns a table has follows:

    root_name down1_name down2_name down3_name
    animal birdie NULL NULL
    animal doggie companion chihuahua
    animal doggie companion poodle
    animal gerbil NULL NULL
    mineral feldspar NULL NULL
    mineral silica NULL NULL
    vegetable carrot NULL NULL

    I need to transform this table in a list as follows:

    animal
    birdie
    doggie
    companion
    chihuahua
    poodle
    gerbil
    mineral
    feldspar
    silica
    vegetable
    carrot

    The list can be formed with ul and li ...

    My first problem is how to find distinct values in root_name to create
    the root items.
    Then going fow from there ...

    Could someone help me out with this?

    Thanks,
    Miguel
  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: Loop

    shapper wrote:
    Hello,
    >
    I created a Linq query which returns a table has follows:
    >
    root_name down1_name down2_name down3_name
    animal birdie NULL NULL
    animal doggie companion chihuahua
    animal doggie companion poodle
    animal gerbil NULL NULL
    mineral feldspar NULL NULL
    mineral silica NULL NULL
    vegetable carrot NULL NULL
    >
    I need to transform this table in a list as follows:
    >
    animal
    birdie
    doggie
    companion
    chihuahua
    poodle
    gerbil
    mineral
    feldspar
    silica
    vegetable
    carrot
    >
    The list can be formed with ul and li ...
    >
    My first problem is how to find distinct values in root_name to create
    the root items.
    Then going fow from there ...
    >
    Could someone help me out with this?
    >
    Thanks,
    Miguel
    If you made sure that the records are sorted on the root_name, you can
    just keep track of the current name. Like:

    string currentRoot = null;
    foreach (something item in someList) {
    if (currentRoot != item.root_name) {
    if (currentRoot != null) {
    // output end of ul tag
    }
    currentRoot = item.root_name;
    // output currentRoot
    // output start of ul tag
    }
    // output li tags
    }
    if (currentRoot != null) {
    // output end of ul tag
    }


    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • shapper

      #3
      Re: Loop

      On Jun 26, 11:08 am, Göran Andersson <gu...@guffa.co mwrote:
      shapper wrote:
      Hello,
      >
      I created a Linq query which returns a table has follows:
      >
      root_name     down1_name   down2_name   down3_name
      animal         birdie              NULL          NULL
      animal        doggie               companion             chihuahua
      animal        doggie               companion       poodle
      animal        gerbil               NULL           NULL
      mineral       feldspar             NULL           NULL
      mineral       silica               NULL          NULL
      vegetable     carrot               NULL           NULL
      >
      I need to transform this table in a list as follows:
      >
      animal
          birdie
          doggie
              companion
                  chihuahua
                   poodle
          gerbil
      mineral
          feldspar
          silica
      vegetable
          carrot
      >
      The list can be formed with ul and li ...
      >
      My first problem is how to find distinct values in root_name to create
      the root items.
      Then going fow from there ...
      >
      Could someone help me out with this?
      >
      Thanks,
      Miguel
      >
      If you made sure that the records are sorted on the root_name, you can
      just keep track of the current name. Like:
      >
      string currentRoot = null;
      foreach (something item in someList) {
          if (currentRoot != item.root_name) {
             if (currentRoot != null) {
                // output end of ul tag
             }
             currentRoot = item.root_name;
             // output currentRoot
             // output start of ul tag
          }
          // output li tags}
      >
      if (currentRoot != null) {
          // output end of ul tag
      >
      }
      >
      --
      Göran Andersson
      _____http://www.guffa.com
      Hi,

      Well, in my Linq query I have ordered it by root_name.

      Let me explain what I am doing. I am implementing in C# using LINQ the
      adjacency model to hold some categories and subcategories:


      I need to create the following, which I think it will have the same
      implementation:

      1. Create a SelectList where each list item is filled with:
      Name = Node Name (root_name, down1_name, ...)
      Value = Node Id (root_id, down1_id, ...)

      If the node is on root its name is as it is.
      If the node is first level one @nbsp; should be added to the
      beginning of its name.
      If the node is second level two @nbsp; should be added to the
      beginning of its name.

      This is similar to what WordPress uses in Category selection.

      2. Create an unordered list to display a site map.

      I am adding the ID so I can edit categories.

      Anyway, I already created the LINQ query which I think it is ok:

      var categories = from root in database.Catego ries
      where root.parentid = null
      orderby root.name, down1.name, down2.name,
      down3.name
      join down1 in database.Catego ries on
      down1.parentid equals root.id
      join down2 in database.Catego ries on
      down2.parentid equals down1.id
      join down3 in database.Catego ries on
      down3.parentid equals down2.id
      select new {
      root_id = root.id,
      root_name = root.name,
      down1_id = down1.id,
      down1_name = down1.name,
      down2_id = down2.id,
      down2_name = down2.name,
      down3_id = down3.id,
      down3_name = down3.name
      };

      In this moment I am having problems with the loop.

      I hope this time I explained everything well ... :-)

      Any help is welcome.

      Thanks,
      Miguel

      Comment

      Working...