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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:48:57+00:00 2026-05-27T22:48:57+00:00

Often with lists and the items inside them, I find the need to query

  • 0

Often with lists and the items inside them, I find the need to query or find things in the list the object has been added to.

For example:

// create list of levels
IList<Levels> levels = new List<Levels>
{
    levels.Add(new Level{ Depth = 1, Name = "Level 1" });
    levels.Add(new Level{ Depth = 2, Name = "Level 2" });
}

foreach(Level level in levels)
{
    bool lowestLevel = false;

    // would the lowest level be best worked out from a function that takes the list and the level?
    lowestLevel = Level.GetLowest(Levels, level);

    // as a calculated property of level itself where the level knows about the list of levels it's in?
    lowestLevel = Level.IsLowest;

    // or worked out when creating the level?
    // levels.Add(new Level{ Depth = 1, Name = "Level 1", IsLowest = isLowest });
    lowestLevel = Level.IsLowest;
}

Are any of these a ‘best practice’ way to handle something like this or is there another way?

Thanks in advance.

  • 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-05-27T22:48:58+00:00Added an answer on May 27, 2026 at 10:48 pm

    Ignoring the fact that adding to the collection being iterated over throws an exception…

    There is definitely another way. When a Level needs to know about its siblings, you should encapsulate levels inside a class, say LevelCollection. You can give each Level a reference to its parent collection when you insert a level into a collection, and stop passing levels in the methods than need the siblings.

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

Sidebar

Related Questions

I often need to list items with various-sized images on the left and text
Often you need to show a list of database items and certain aggregate numbers
Here at work, we often need to find a string from the list of
Often times it seems I have a list of items, and I need to
I often find myself with a list of disconnected Linq2Sql objects or keys that
I've got a class that contains (among many other things) a list of items
When I am writing code in Python, I often need to remove items from
I find myself often using a pattern where I transform a list with a
A decision I often run into is when to sort a list of items.
I often find myself building a list item row or something by grabbing a

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.