-2

I have a tree stucture like this

public class CustomObject
    {
        public int Property1{get;set;}
        public string Property2{get;set;}
        public bool IsTrue{get;set;}
        public List<CustomObject> Items{get;set;}
    }

Now I have Collection like List and I want create a new collection from this where IsTrue == true.

Secondly I need to add new Property to the stucture at specific node and similary delete and Update the nodes.

Any suggestion is greatly appreciated.

Crispin
  • 25
  • 3
  • IsTrue is a propoerty – Crispin Apr 28 '16 at 12:38
  • You should add some mock code to explain what exactly you are trying to do. The second part of the question is a bit vague too. – Manfred Radlwimmer Apr 28 '16 at 12:44
  • Suppose i have collection like this Item1 ---> First Item In Collection Item11 -----> Fisrt Item1 Collection Item12 -----> Second Item1 Collection Item21 ----> First item in Item2 Collection and so on Item23 Item2 ---> Second item in collection ... So On I want to add items at any one of the nodes or update any node and so on – Crispin Apr 28 '16 at 12:45
  • Yes, that part is mentioned in the question. Now what do you want to do with that collection? Whats stopping you from Deleting / Updating the Nodes? What's the problem? You got a List<> of your Elements. Lists support _Remove_. The Values are not readonly properties so updating shouldn't be a problem either. – Manfred Radlwimmer Apr 28 '16 at 12:48

2 Answers2

0

First

Now I have Collection like List and I want create a new collection from this where IsTrue == true.

var listWithIsTrueOnly = ListCustomObject.Where(x => x.IsTrue).ToList()

And now for your list you can use Add, AddRange, Remove, RemoveAt ...

But still something curious. In your object, you have a list of object. Is it normal?

dpfauwadel
  • 3,866
  • 3
  • 23
  • 40
0

Items.Where(x => x.IsTrue).ToList().

Over the list you can perform crud operations.

If you want to add an extra property at specific node, I suggest you consider to use an ExpandoObject instead, so you can add or remove the properties at runtime.

Check this links :

Dynamically Add C# Properties at Runtime

https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx

Community
  • 1
  • 1
Zinov
  • 3,817
  • 5
  • 36
  • 70