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 8966499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:02:15+00:00 2026-06-15T17:02:15+00:00

I’m trying to update an item inside my object graph which could span ‘n’

  • 0

I’m trying to update an item inside my object graph which could span ‘n’ level in depth.

The following is my object model:

   public class Entity
    {
        public string Name { get; set; }
    }

    public class Category : Entity
    {        
        public List<Category> Categories { get; set; }
        public List<Product> Products { get; set; }      
    }

    public class Product : Entity
    {        
    }

My view is bound to an ObservableCollection<Category> Categories. What I want to do is given a category name, I need to retrieve the first object which matches that from the collection.

For ex, given a list like this and a category Facial Tissue, I need to retrieve the Facial Tissue category object from the collection.

Category - Pharmacy
  |-Product - Aspirin
  |-Product - Tylenol
  |-Category - Tooth Paste
  |  |-Product - Crest
  |  |-Product - Colgate
  |-Category - Paper Products
   |-Category - Toilet Paper
   |  |-Product - NoName
   |  |-Product - Charmin
   |-Category - Facial Tissue
      |-Product - Kleenex
Category - Household
  |-Product - Pinesol Cleaner
  |-Product - Garbage Bags

I have tried this, but it throws an Object reference not set to an instance of an object exception when I search level >2 in the hierarchy.

 return Categories.FirstOrDefault(n => n.Name == name) ??
                   Categories.SelectMany(node => node.Categories).Where(lx => lx.Name == name).FirstOrDefault();

NOTE: At times the categories could be null deep down the hierarchy.i.e if there are no categories, then the collection is set to null.Also the solution necessarily need not be using LINQ.

  • 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-15T17:02:16+00:00Added an answer on June 15, 2026 at 5:02 pm

    You can use either of the following methods to traverse the tree structure recursively:

    public static IEnumerable<T> Traverse<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> childSelector)
    {
        var queue = new Queue<T>(source);
        while (queue.Any())
        {
            var item = queue.Dequeue();
            yield return item;
            foreach (var child in childSelector(item))
            {
                queue.Enqueue(child);
            }
        }
    }
    
    public static IEnumerable<T> Traverse<T>(T root, Func<T, IEnumerable<T>> childSelector)
    {
        return Traverse(new[] { root }, childSelector);
    }
    

    There is an overload for a single root item, and another that takes a sequence of items.

    You could implement them using actual recursion if you prefer, but I prefer an explicit data structure. If you would like a depth first search instead of a breath first search just change the Queue to a Stack and update the methods accordingly.

    To use it you can do something like this:

    Category root = new Category();
    var searchResult = Traverse(root, item => item.Categories)
                .Where(category => category.Name == "testValue")
                .FirstOrDefault();
    

    It also appears that you’re getting null errors because you have Categories that are null. If at all possible, I would highly encourage you to fix that problem, rather than dealing with it. If an Entity has no Categories it should have an empty list, not a null list. Having said that, you can adjust the Traverse calls as follows if you have any null items:

    Traverse(root, item => item.Categories ?? Enumerable.Empty<Category>())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.