QueryAll
This method queries all rows from a table.
Code Snippets
The following example fetches all rows from the [dbo].[Person] table.
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll<Person>();
}
Targeting a Table
To target a specific table, pass the literal table name.
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll<Person>("[dbo].[Person]");
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll("[dbo].[Person]");
}
The result is a list of dynamic objects of type ExpandoObject.
Specific Columns
To query specific columns, pass a list of fields in the fields argument.
using (var connection = new SqlConnection(connectionString))
{
var fields = Field.Parse<Person>(e => new
{
e.Id,
e.Name,
e.DateOfBirth,
e.DateInsertedUtc
});
var people = connection.QueryAll<Person>(fields: fields);
}
Or via dynamics:
using (var connection = new SqlConnection(connectionString))
{
var people = connection.Query("[dbo].[Person]",
fields: Field.From("Id", "Name", "DateOfBirth", "DateInsertedUtc"));
}
Type Result
The result set can be inferred directly as a string column.
using (var connection = new SqlConnection(connectionString))
{
var names = connection.QueryAll<string>(ClassMappedNameCache.Get<Person>(),
fields: Field.From(nameof(Person.Name))).FirstOrDefault();
}
Type inference works for string but not for other non-class types (e.g., long, int, System.DateTime), since TEntity is constrained to class. Use ExecuteQuery for those types.
Table Hints
Pass a table hint via the hints argument.
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll<Person>(hints: "WITH (NOLOCK)");
}
Or use the SqlServerTableHints class.
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll<Person>(hints: SqlServerTableHints.TabLock);
}
Ordering the Results
Pass an array of OrderField objects in the orderBy argument.
using (var connection = new SqlConnection(connectionString))
{
var orderBy = OrderField.Parse(new
{
LastName = Order.Descending,
FirstName = Order.Ascending
});
var people = connection.QueryAll<Person>(orderBy: orderBy);
// Process 'people' here
}
Caching the Results
Pass a literal string key in the cacheKey argument to cache the results.
using (var connection = new SqlConnection(connectionString))
{
var people = connection.QueryAll<Person>(cacheKey: "CackeKey:AllPeople");
// Process 'people' here
}
The default cache expiration is 180 minutes. Override it by passing an integer value in the
cacheExpirationInMinutesargument.