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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:09:59+00:00 2026-05-26T11:09:59+00:00

sometimes, i just feel dumb… i have a simple class: public class myClass {

  • 0

sometimes, i just feel dumb…

i have a simple class:

public class myClass
{
    public long Id { get; set; }
    public long ParentChannelId { get; set; }
}

and i have a list that contains the class:

List<myClass> myItems = new List<myClass>

further down the code, i feed the list with classes.

now, i want to delete an item from the list.
but, since an item can have children and grandchilds etc…
i want to delete everything related..

was thinking of something like:
(pseudo code )

var List<myClass> itemsToDelete = myItems.Where(i => i.Ancestors.Contains(myItemId));

but i dont really have the brains atm to know how to write it exactly… :\
i do have the .Ancestors function…
just need help with the lambda linq

public List<Channel> Ancestors
{
    get
    {
        List<MyCms.Content.Channels.Channel> result = new List<MyCms.Content.Channels.Channel>();

        Channel channel = this;

        while (channel != null)
        {
            result.Add(channel);
            channel = myChannels.Where(c => c.ParentChannelId == this.Id).First();
        }
        result.Reverse();

        return result;
    }
}

EDIT: guess i did not explain myself as i should…
i have all the properties like ancestors, children parent etc…
i want to select all the classes that might contain the specific class…

  • 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-26T11:09:59+00:00Added an answer on May 26, 2026 at 11:09 am

    I’ve re-read your question, especially the last part where you said you already have .Ancestors, and now it makes more sense.

    Do this to get your list of items to delete:

    List<MyClass> itemsToDelete = myItems
        .Where(i => i.Id == myItemId)
        .SelectMany(i => i.Ancestors)
        .Concat(myItems) // Want to delete these too, not just the ancestors
        .ToList()
        ;
    

    Then you can foreach through the result, and remove them from the original list.

    I’d suggest keeping these in a Dictionary<int, MyClass> or a HashSet<MyClass> instead of a list, since removal will be way faster.

    For a HashSet, you’ll have to implement Equals and GetHashCode, or create an IEqualityComparer<MyClass> implementation to provide those methods.

    Before Edit:

    I wouldn’t write my code this way. I’d simply create a Dictionary<int, MyClass> instead of a list. It will do a lookup way faster than anything involving ancestors/tree traversal.

    But here is how to accomplish what you’re trying to accomplish:

    If you’re using Linq to Objects (as opposed to Linq to SQL or Linq to Entities), make a property called Parent on MyClass, of the correct type, instead of trying to link them by Id.

    Then you can make an Ancestors property fairly easily:

    public IEnumerable<MyClass> Ancestors
    {
        get
        {
            MyClass current = this;
    
            while(current != null)
            {
                current = current.Parent;
                yield return current;
            }
        }
    }
    

    If you can’t edit the class, make an extension method called GetAncestors.

    Then you can use something very similar to the code you wrote in your question:

    List<MyClass> itemsToDelete = myItems
        .Where(i => i.Ancestors.Any(a => a.Id == myItemId))
        .ToList();
    

    Linq to Entities

    If you are using Linq to Entities, create a navigation property of the type MyClass to navigate to the parent, and do the same thing. Note that this might cause re-queries. Not sure the Linq can or would get translated into a hierarchical query.

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

Sidebar

Related Questions

I'm trying to get a query working that takes the values (sometimes just the
The location text from Twitter could be just about anything. Sometimes Twitter clients set
I have a PHP script that is running and I sometimes just want to
Sometimes I feel I can't write a simple line of code without using an
Sometimes I need to find some strings inside DB usually it is just host-name
I just basically want to add about 20 and sometimes 80 Proximity Alerts with
Sometimes I want to build Python or GCC from scratch just for fun, but
Sometimes it might be useful, but mostly just looking cool or impressive to visualize
Are there any tools for just browsing SQL Server? I ask because sometimes SSMS
In a comment I read Just as a side note, it's sometimes faster to

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.