drawalegna
asked on
How do I query a nested collection using LINQ
I have a collection of nested collections which can contain an unlimted number of nested collections. A code snipet below shows the class structure. I need to query the collection by ID to find a match. Sometime I need the Object where the ID was found and at others I need the parent. I'm new to LINQ and cannot determine how to make the query traverse all levels of the collection. Can someone assist me with this query?
Also given this basic query, I cannot actually query s.Values because s is the base class which does not contain the Values collection. I assume I can type cast the query, but can you address this in the query as well?
The basic query
var Segment = from s in Segments
where s.ID == 4 && s.Values[2]=='OB'
select s;
Thanks
Also given this basic query, I cannot actually query s.Values because s is the base class which does not contain the Values collection. I assume I can type cast the query, but can you address this in the query as well?
The basic query
var Segment = from s in Segments
where s.ID == 4 && s.Values[2]=='OB'
select s;
Thanks
class FileDocument
{
public List<Section> Segments = new List<Section>();
public class Section
{
public int ID;
public string Name;
}
public class FileSegment : Section
{
public List<string> Values = new List<string>();
}
public class FileLoopGroup : Section
{
public List<FileLoop> LoopGroup = new List<FileLoop>();
}
public class FileLoop
{
public List<Section> Sections= new List<Section>();
}
}
Example:
Segments
FileSegment ID=1
FileSegment ID=2
FileLoopGroup ID=3
FileLoop
FileSegment ID=4 Values[2]="OB"
FileSegment ID=5
FileLoopGroup ID=6
FileLoop
FileSegment ID=7
FileSegment ID=8
FileLoop
FileSegment ID=4 Values[2]="ST"
FileSegment ID=5
FileSegment ID=9
Query 1 needs to return the FileSegment where the match was found.
For example: where s.ID == 4 && s.Values[2]=='OB'would return the first FileSegment
Query 2 needs to return the FileLoop that contains the matching Segment.
For example: where s.ID == 4 && s.Values[2]=='ST'would return the second FileLoop
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER