Lawrence,
You want to use the following
Story story = StoryProvider
.GetStory(id)
.Columns("StoryID", "Title", "BGColor", "Summary", "VideoId", "Description", "Image", "AniProp")
.OrderBy("NodeOrder")
.TopN(1)
.TypedResult // This is Kentico method
.FirstOrDefault(); // This is a System.Linq method
All of the auto-generated classes for each Page Type you create will work with DocumentQuery<T>
where T
is your custom Page Type.
You can learn more about how to use DocumentQuery<T>
in the documentation
https://docs.kentico.com/k12sp/custom-development/working-with-pages-in-the-api
https://docs.kentico.com/k12sp/custom-development/working-with-pages-in-the-api/reference-documentquery-methods
There are a lot of methods and not all of them are in the documentation, but here are a couple of blog posts that might help:
https://carly.io/2016/10/05/kentico-9-data-query-api-examples/
https://dev.to/seangwright/kentico-12-design-patterns-part-14-documentquery-and-objectquery-tips-2c7h (shameless plug for my own blog)
The DocumentQuery<T>
data access pattern was added to Kentico back in Kentico 8. Here is a post about it when it was first released that might also be helpful:
https://devnet.kentico.com/articles/kentico-8-technology-documentquery-api
In the code snippet I provided above you can see I used a combination of Kentico's querying methods and System.Linq
. There is also a .FirstObject
property that can be found when working with DocumentQuery<T>
, but it has been deprecated and using .FirstOrDefault();
is the recommended approach.
You can see I used .TopN(1)
as well. This isn't required, but it is a good practice to use this method when you want only 1 result. .FirstOrDefault();
gives you the first object of the collection in memory, whereas .TopN(1)
ensures only 1 result is brought back from the database to memory.