Accessing the first item in a result

Sometimes you want only the first item in a result, regardless of how many instances the database can provide. Use the FirstResult property on a result object to obtain its first item, as the following Gosu code shows.

uses gw.api.database.Query 

var query = Query.make(Person)

query.compareIgnoreCase(Person#FirstName, Equals, "ray")
query.compareIgnoreCase(Person#LastName, Equals, "newton")

firstPerson = query.select().FirstResult

As an alternative, you can iterate a result and stop after retrieving the first item. However, relational query performance often improves when you use the FirstResult property to access only the first item in a result.

Note: To find the last item in a result, reverse the order of the rows by calling the orderByDescending method.