Combining Strong Typing with custom query data

Posted: 10/28/2022 5:35:21 PM by Chris Bass | with 0 comments
In the past, whenever I've tried to write a query that has calculated fields (or aggregates, joins, etc) I've found you had to work with a bare DataSet rather than doing .TypedQuery, because TypedQuery would only set the fields that match the base Page Type - even object.GetStringValue() wouldn't get the columns that weren't 'part of' that Page Type.
But it seems to work if I do this:
var outputtedData = myDocumentQuery
                .Result.Tables[0].Rows.OfType<DataRow>()
                .Select(row =>
                {
                    MyPageTypeInfo record = MyPageTypeInfo.New(row); //Here, we inject the DataRow into a local object
                    return
                        new
                        {
                            record.DocumentName,
                            record.DocumentPageDescription,
                            record.RelativeURL,    //We can use Kentico's calculated fields like RelativeURL, which aren't in the database directly
                            record.DocumentSearchExcluded,
                            MaxArticleDate = row.Field<DateTime?>("MaxArticleDate") //But we can also manually get custom values, such as a custom max-Date field, out of the DataRow directly
                        };
                });
Naturally, whatever query you're using has to specify all the relevant columns (so for RelativeURL you need NodeAliasPath, etc in the DataRow) but by converting the DataRow into an object, you can use the calculated data (and strong typing) alongside custom fields (in the above case, it was an aggregation subquery) (edited) 
Comments
Blog post currently doesn't have any comments.
 Security code