Method BindResults
- Namespace
- YndigoBlue.Velocity.Engine
- Assembly
- YndigoBlue.Velocity.dll
BindResults<T>(BindingType)
Binds query results to a typed enumerable collection using the specified binding strategy.
public IEnumerable<T> BindResults<T>(BindingType bindingType) where T : new()
Parameters
bindingTypeBindingTypeThe binding strategy to use (DirectMap, SnakeCase, CamelCase, or Attribute).
Returns
- IEnumerable<T>
An enumerable collection of typed objects populated from the query results.
Type Parameters
TThe type to bind each row to. Must have a parameterless constructor.
Examples
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime CreatedAt { get; set; }
}
var conn = new PostgreSqlDatasourceConnection
{
Hostname = "localhost",
Database = "app_db",
Username = "user",
Password = "password"
};
using (var manager = new Manager(conn))
{
var schema = manager.LoadSchema("public");
var usersTable = schema["users"];
var query = new Query()
.Select([usersTable["id"], usersTable["name"], usersTable["email"], usersTable["created_at"]])
.From(usersTable);
// Stream results with minimal memory usage
using (ResultReader reader = manager.ExecuteReader(query))
{
// Use SnakeCase to map created_at -> CreatedAt
foreach (User user in reader.BindResults<User>(BindingType.SnakeCase))
{
Console.WriteLine($"{user.Name} - {user.Email}");
}
}
}
Remarks
Binding Types:
- DirectMap - Maps properties directly to columns with exact name matching (case-insensitive)
- SnakeCase - Converts C# PascalCase properties to snake_case database columns (e.g., FirstName -> first_name)
- CamelCase - Converts C# PascalCase properties to camelCase database columns (e.g., FirstName -> firstName)
- Attribute - Uses [VelocityField] attributes to map properties to custom column names