Link to home
Start Free TrialLog in
Avatar of JRockFL
JRockFLFlag for United States of America

asked on

Iterate two lists and remove inactive

I need to iterate two lists and remove inactive or create a new list with just active.
 It is a parent/child relation.
 
IList<string> activeIDs; this contains the list of active ids.
If the id in ClassificationDetail matches, then include this in the list.
 
 
	public class Classification
    {
        public List<Classification> Children { get; set; }
        public ClassificationDetail Classification { get; set; }
    }
	
	public class ClassificationDetail
	{
		string id {get; set;}
	}
	
	IList<Classification>

Open in new window

Avatar of Paweł
Paweł
Flag of Switzerland image

you can use linq to pull this off, just use a where clause to get all the elements in your  IList<Classification> that have their ids in your activeIDs list, here's a console app for you

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class ClassificationDetail
    {
        public string id { get; set; }
    }

    class Classification
    {
        static int _runningId = 0;
        public List<Classification> Children { get; set; }
        public ClassificationDetail ClassificationDetail { get; set; }

        public Classification()
        {
            this.ClassificationDetail = new ClassificationDetail { id = (_runningId++).ToString() };
        }

        public override string ToString()
        {
            return  $"Classification with ID:{this.ClassificationDetail.id}";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IList<string> activeIDs = new List<string>(new List<string> { "0", "1", "3", "8" });

            IList<Classification> classifications = new List<Classification>();

            //populate list
            for (var i = 0; i < 10; i++)
                classifications.Add( new Classification());

            var result = classifications.Where(c => activeIDs.Contains(c.ClassificationDetail.id));

            foreach(var r in result)
                Console.WriteLine(r);
        }
    }
}

Open in new window

Avatar of JRockFL

ASKER

Thank you for the reply. Will this take into consideration the parent child relationships?
I need to traverse list.
Avatar of Dmitry G
From what I see - the answer to your question is "No".

It seems the task might be a bit tricky. And I'd like to clarify it - the question is not clear..

As I understand every Classification object contains a list of "child" Classification objects. Therefore, in it's turn, every child classification may have it's own children? Yes or no? And every "grandchild" may have own children as well? How deep this can be?

The second question is: what Classification  objects do we select for your "selected" list? Only "parent" Classification objects that have active IDs or at least one of its children/grandchildren/grandgrandchildren/... has active IDs? Or children/grandchildren/grandgrandchildren themselves?

Will try to explain on an example.
. Scenario 1.
Parent CP1 has children CC1,CC2,CC3
CP1 is actice, and CC2 is active.
What classifications do you include into the list? CP1 only? Or CP1 and CC2?

. Scenario 1.
Parent CP1 has children CC1,CC2,CC3
CP1 is inactice, only CC2 is active.
What classifications do you include into the list? CP1? Or CC2?

You may have more scenarios with grandchildren, etc.
Avatar of JRockFL

ASKER

Yes, that is correct. There is a root object that contains 48 children. Each of those child may or not have children and it can go indefinitely.
The classificationdetail object has a property called Id. If that Id matches one of the ids in the "active" list. Then it is considered active.

CP1 and CC2 would each have an "active" id in the list.
Sorry, I called both scenarios "Scenario1" :). For the second one, what objects do we include to the "selected" list? Do we consider CP1 active if it has NOT active ID but one of his children/grandchildren/... has active ID?

outcome 1: We include both CP1 into "final" list because it has "active" children + CC2 as it has active ID
outcome 2: We include only"active" child CC2 as it has active ID
Avatar of JRockFL

ASKER

That scenario should never happen.
It would be much easier if you explain the problem clearly :).

OK, another question about the Scenario1.
What do we include into the final list? CP1 and CC2? Or only CP1?

What is our "starting point"?
I know that we have IList<string> activeIDs. This is clear.

What about Classification? You have just one Classification object with children? Or you have a collection of classifications, aka "parents"?
One can't get a solution if requirements are not clear!..
Avatar of JRockFL

ASKER

We start off with a collection of Classification
IList<Classification>  with a count of 1

This root ojbect has 49 children.
Some of these children have children.
So of these children do not have children.
And this continues with their children.

Then we ClassificationDetail classs. Specifically, the id.
If that id is in our active list. Then this is active.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JRockFL

ASKER

Thank you. I will review tomorrow
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial