By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

Loop

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jun 27 '08 #2

P: n/a
On Jun 26, 11:08*am, Göran Andersson <gu...@guffa.comwrote:
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:
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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.