Loop

S

shapper

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
 
G

Göran Andersson

shapper said:
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
}
 
S

shapper

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

}

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:
http://www.sqllessons.com/categories.html

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.Categories
where root.parentid = null
orderby root.name, down1.name, down2.name,
down3.name
join down1 in database.Categories on
down1.parentid equals root.id
join down2 in database.Categories on
down2.parentid equals down1.id
join down3 in database.Categories 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq. Join 2

Top