Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8465027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:55:31+00:00 2026-06-10T14:55:31+00:00

Having these 2 classes and this linq request: var cats = from c in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:55:33+00:00Added an answer on June 10, 2026 at 2:55 pm

    Time for a recursion! Simply wrap the function getting the Category and then just call it when needed.

    public static IQueryable<Category> GetCategories(string catName, XDocument xml)
    {
          var cats = from c in xml.Descendants("category")
                     let categoryName = c.Attribute("name").Value
                     let descendants = c.Descendants()
                     where (catName == "" || categoryName == catName)
                     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 = GetCategories(categoryName, xml).FirstOrDefault()
                                  }
                    };
    
           return cats.AsQueryable();
    }
    

    And you call it like this:

    XDocument xml = XDocument.Parse(...); // parse the xml file
    IQueryable<Category> cats = GetCategories("", xml);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
I have been having these really odd problems with Visual Studio 2010. At this
I'm new to LINQ , and I'm having trouble organizing this query to return
I'm having troubles keeping classes from being regenerated. I'm I have a schema A,
Having used optional parameters in a few classes here and there, I'm starting to
Having these generic interface and class: interface TestIntf<T extends TestIntf<T>> { void test(T param);
I need a control having these features: It should be possible use it in
Using jQuery timepickr: http://archive.plugins.jquery.com/project/jquery-timepickr Having included these files: jquery-1.7.1.min.js jquery-ui-1.8.21.custom.min.js ui.timepickr.js And getting this
Keeping these in mind - HttpContext.Current - Foreach I'm having trouble wrapping my head
I'm having the same problem issued in these two links: one , two .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.