Dapper - QueryFirst

Description

QueryFirst method is an extension method which can be called from any object of type IDbConnection. It can execute a query and map the first result.

The result can be mapped to:

Parameters

The following table shows different parameter of an QueryFirst method.

Name Description
sql The query to execute.
param The query parameters (default = null).
transaction The transaction to use (default = null).
commandTimeout The command timeout (default = null)
commandType The command type (default = null)

First, Single & Default

Be careful to use the right method. First & Single methods are very different.

Result No Item One Item Many Items
First Exception Item First Item
Single Exception Item Exception
FirstOrDefault Default Item First Item
SingleOrDefault Default Item Exception

Example - Query Anonymous

Execute a query and map the first result to a dynamic list.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoice = connection.QueryFirst(sql, new {InvoiceID = 1});
}

Example - Query Strongly Typed

Execute a query and map the first result to a strongly typed list.

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    var invoice = connection.QueryFirst<Invoice>(sql, new {InvoiceID = 1});
}