C# Generic collections - Get list of sub level child items from parent object

chagantivamsi87

New member
Joined
Dec 24, 2021
Messages
3
Programming Experience
10+
C#:
Public class viewmodel
{
    public string ID {get;set;}
    public String name{get;set;}
    public List<child1>{get;set;}
    
}
    public class child1
    {
        public string C1ID {get;set;}
        public String C1name{get;set;}
        public List<child2>{get;set;}
    }
        public class child2
        {
        public string C2ID {get;set;}
        public String C2name{get;set;}
        public child3{get;set;}
        }

Now I need List<Child3> when I pass ViewModel object as input paramenter
I did in this way and its working fine also but not sure its good one or tune further
viewModleObject.child1.Select(item => item.child2)
                                            .FirstOrDefault().Where(t => (t.operation.ToString().Contains("Delete")))
                                                .Select(q => q.child3).ToList();
I have parent object which contain List<child1> and this child1 has List<child2> , I would like to get the List<Child2> items as return type when I pass Parent object as input parameter
It should not have multiple loops.



Thanks,
Krishna
 
No loops in your code? Or no loops at all? Using SelectMany() uses loops internally -- you just don't see it in your code.
 
C#:
Public class viewmodel
{
    public string ID {get;set;}
    public String name{get;set;}
    public List<child1>{get;set;}
   
}
    public class child1
    {
        public string C1ID {get;set;}
        public String C1name{get;set;}
        public List<child2>{get;set;}
    }
        public class child2
        {
        public string C2ID {get;set;}
        public String C2name{get;set;}
        public child3{get;set;}
        }

Now I need List<Child3> when I pass ViewModel object as input paramenter
I did in this way and its working fine also but not sure its good one or tune further
viewModleObject.child1.Select(item => item.child2)
                                            .FirstOrDefault().Where(t => (t.operation.ToString().Contains("Delete")))
                                                .Select(q => q.child3).ToList();
I have parent object which contain List<child1> and this child1 has List<child2> , I would like to get the List<Child2> items as return type when I pass Parent object as input parameter
It should not have multiple loops.



Thanks,
Krishna
The code you shared is incomplete / laced with errors.
Its not clear what you're trying to accomplish.

I suggest you start by posting a working example of your code, and then describe what you're trying to accomplish with the Linq statement.
 
Back
Top Bottom