Having these 2 classes and this linq request:
var cats = from c in xml.Descendants("category")
let categoryName = c.Attribute("name").Value
let descendants = c.Descendants()
select new Category
{
Name = categoryName,
Items = from d in descendants
let typeId = d.Attribute("id").Value
select new Item
{
Id = typeId,
Name = d.Value,
Category = ???????
}
};
class Category
{
string Name;
IEnumerable<Item> Items;
}
class Item
{
string Id;
string Name;
Category Category;
}
How can I affect the item’s category to the current selected category?
A kind of a keyword like this maybe?
Time for a recursion! Simply wrap the function getting the
Categoryand then just call it when needed.And you call it like this:
First load of the function calls uses empty string as category name, because we don’t need to filter the results. Then we recursively call the same function, but filtering by category name. Give it a try, worked for me.