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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:32:55+00:00 2026-05-27T06:32:55+00:00

So I have a collection of objects in one list, but each object in

  • 0

So I have a collection of objects in one list, but each object in that list contains another list.

Consider the following:

class Parent
{
    public Parent(string parentName)
    {
        this.ParentName = parentName;
    }
    public string ParentName { get; set; }
    public List<Child> Children { get; set; } 
}
class Child
{
    public Child(string name)
    {
        this.ChildName = name;
    }

    public string ChildName { get; set; }
}

By the nature of the application, all Parent objects in the list of parents are unique. Multiple parents can contain the same child, and I need to get the parents that contain child x.

So, say the child with ChildName of “child1” belongs to both parents with ParentName of “parent1” and “parent5”. If there are 100 parents in the collection, I want to get only the ones that have the Child with ChildName of “child1”

I would prefer to do this with a lambda expression but I’m not sure where to start as I don’t really have to much experience using them. Is it possible, and if so, what is the correct approach?

  • 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-27T06:32:55+00:00Added an answer on May 27, 2026 at 6:32 am

    If the Child class has defined an equality operation by implementing IEquatable<Child>, you can do this easily by using a lambda, the Enumerable.Where method of LINQ and the List.Contains method:

    var parents = new List<Parent> { ... }; // fully populated list of parents
    var child = null; // the child you are looking for goes here
    var filtered = parents.Where(p => p.Children.Contains(child));
    

    You can now iterate over filtered and perform your business logic.

    If the Child class does not have an equality operation explicitly defined (which means that it will use reference equality rules instead of checking for identical ChildName), then you would need to include the “what passes for equal” check into the lambda yourself:

    var filtered = parents.Where(p => p.Children.Any(c => c.ChildName == "child1"));
    

    Note: There are of course many other ways to do the above, including the possibly easier to read

    parents.Where(p => p.Children.Count(c => c.ChildName == "child1") > 0);
    

    However, this is not as efficient as the Any version even though it will produce the same results.

    In both cases, the lambdas very much “read like” what they are intended to do:

    1. I want those parents where the Children collection contains this item
    2. I want those parents where at least one of the Children has ChildName == "child1"
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Queue that contains a collection of objects, one of these objects
I have two Collection objects, I want to associate each object of these two
I have a parent object (part of a DAL) that contains, amongst other things,
I have a worker class that does stuff with a collection of objects. I
I have a collection of objects that all implement one (custom) interface: IAuditEvent .
I have a List<State> of State entity objects, and each State object has collections
I have a collection of custom entity objects one property of which is an
I have one collection numbers with 200000 document object with {number: i} i =
I've looked around and cannot figure this one out: I have a object that
I have a list of objects, each with a bool ShouldRun() method on them.

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.