Is it possible to write this in LINQ? I tried using LINQ. However, I figured it will have to be looped twice; first to verify whether a is present and next to iterate through qs.
So I came up with this code.
public a Traverse(List<q> qs,string id)
{
foreach (var q in qs)
{
if (q.as.Any(a => a.Id == id))
{
return q.as.First(a => a.Id == id);
}
foreach (var a in q.as)
{
var result =Traverse(a.qs, id);
if(result != null)
return result;
}
}
return null;
}
I am reading it from XML which is like "q" have "a" and "a" have "q" in recursive manner.
I need to find unique Id belonging to a.
I know there have been discussions on other threads but they were not helpful.
EDIT: Here's snippet of XML
<qs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<qs>
<q>q 1</q>
<Id>1</Id>
<as>
<a>
<aprop>a</aprop>
<Id>a 1.1</Id>
<qs>
<q>
<q>q 1.1.1</q>
<Id>1.1.1</Id>
<as>
<a>
<a>a</a>
<Id>a 1.1.1.1</Id>
<qs />
<type>1</type>
</a>
<a>
<a>a</a>
<Id>a 1.1.1.2</Id>
<qs />
<type>1</type>
</a>
</as>
</q>
<q>
<q>q 1.1.2</q>
<Id>1.1.2</Id>
<as>
<a>
<a>a</a>
<Id>a 1.1.2.1</Id>
<qs />
<type>1</type>
</a>
<a>
<a>a</a>
<Id>a 1.1.2.2</Id>
<qs />
<type>1</type>
</a>
</as>
</q>