Navigate ParserTree with Linq

Dec 25, 2014 at 8:11 PM
Hi gyus,

I am working on a project where I am using the JsonGrammer supplied in the Samples project.

https://irony.codeplex.com/SourceControl/latest#Irony.Samples/DataGrammars/JsonGrammar.cs

I would like to use Linq to find a specific property that I know right now always exists at the top level. So I don't need a fully recursive method.

Right now I use this expression to find my property:
var propNode = jsonDocAst.Root.ChildNodes.SelectMany(x => x.ChildNodes).FirstOrDefault(y => y.Token.ValueString == "myProperty");
But that only finds the property name node, and not the value node, that lives right next to it. The value node is just a sibling.

Any suggestions on a how to do this in a nice way using Linq?
Jan 13, 2015 at 6:20 PM
Instead try:
var propNode = jsonDocAst.Root.ChildNodes
                                 .SelectMany(x => x.ChildNodes)
                                 .SkipWhile(y => y.Token.ValueString != "myProperty")
                                 .Skip(1).Take(1);