@Stuart,
You would need to change your query to have more than (1) result in your docs
variable.
.Path(nodeAliasPath, PathTypeEnum.Section)
will give you the "document and its child documents" and .Path(nodeAliasPath, PathTypeEnum.Children)
will give you "all child documents".
Replace your .WhereEquals("NodeAliasPath", nodeAliasPath)
with one of those.
When you get your results back you can iterate over them and use the .ClassName
property of the TreeNode
to find out what to cast it to.
foreach(TreeNode node in documents)
{
if (string.Equals(AccordionHeader.CLASS_NAME, node.ClassName, StringComparison.OrdinalIgnoreCase))
{
AccordionHeader header = node as AccordionHeader;
}
// This might also work
if (node is AccordionHeader headerNode)
{
var header = headerNode.Header;
}
}
If you are interested in what SQL is being generated by your DocumentQuery<T>
code, you can use the .GetFullQueryText();
method to return the hydrated query as a string, which you can copy into SSMS or Azure Data Studio to execute.
var query = DocumentHelper.GetDocuments()
.Type("MyCustom.AccordionHeader", q => q
.Columns("PageName", "NodeAliasPath", "Header"))
...
.WhereEquals("NodeAliasPath", nodeAliasPath)
.PublishedVersion()
.Published()
.OrderBy("NodeLevel", "NodeOrder", "NodeName");
string queryText = query.GetFullQueryText();
var documents = query.ToList();
If you find yourself wanting to look at / debug queries often in your code, you could make an extension method like:
public static class MultiDocumentQueryExtensions
{
/// <summary>
/// Prints the SQL that will be generated by the provided <see cref="MultiDocumentQuery" /> to <see cref="Debug"/>. Includes SQL variables and values.
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public static MultiDocumentQuery PrintToDebug(this MultiDocumentQuery query)
{
string queryText = query.GetFullQueryText();
Trace.WriteLine("\r\n");
Trace.WriteLine("--- Query Start ---");
Trace.WriteLine("\r\n");
Trace.WriteLine(queryText);
Trace.WriteLine("\r\n");
Trace.WriteLine("--- Query End ---");
Trace.WriteLine("\r\n");
return query;
}
}
Which could be used as follows:
var documents = ...
.WhereEquals("NodeAliasPath", nodeAliasPath)
.PublishedVersion()
.Published()
.OrderBy("NodeLevel", "NodeOrder", "NodeName")
.PrintToDebug()
.ToList();
If you are debugging when the query gets executed, you will see the full text in Visual Studio's Output window.