DataBuddy, Graffiti's data access engine, exposes a Query object allowing you to retrieve data from your site in a variety of ways.
Although the following examples show how to query Comment and Post, you can query almost any object in Graffiti, including Category, Comment, Log, ObjectStore, PostStatistic, Post, Tag, User, and VersionStore.
public PostCollection GetSomePosts()
{
// this will store the results of our query
PostCollection postCollection = new PostCollection();
// this creates the default query for the Post object
// once this query is created, we can apply filters as shown below
Query postQuery = Post.CreateQuery();
// we want to retrieve only the top 10 posts
postQuery.Top = "10";
// you only want Chuck's posts...
postQuery.AndWhere(Post.Columns.CreatedBy, "Chuck");
// or maybe Tommy's posts too...
postQuery.OrWhere(Post.Columns.CreatedBy, "Tommy");
// execute the query and store our results in the postCollection object
postCollection.LoadAndCloseReader(postQuery.ExecuteReader());
// return the populated object
return postCollection;
}
public CommentCollection GetSomeComments()
{
// this will store the results of our query
CommentCollection commentCollection = new CommentCollection();
// this creates the default query for the Comment object
// once this query is created, we can apply filters as shown below
Query commentQuery = Comment.CreateQuery();
// only return published comments
commentQuery.AndWhere(Comment.Columns.IsPublished, true);
// only return comments for a single post (assuming this post exists)
commentQuery.AndWhere(Comment.Columns.PostId, new Data().GetPost("My Favorite Post").Id);
// order the comments by the published date
commentQuery.OrderByDesc(Comment.Columns.Published);
// execute the query and store our results in the commentCollection object
commentCollection.LoadAndCloseReader(commentQuery.ExecuteReader());
// return the populated object
return commentCollection;
}