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>
A lazy IEnumerable<T> that streams typed objects from the database one row at a time.
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
Returns a lazy enumerable — must be consumed inside the using block.
This method uses yield return internally and does not materialise any rows until the returned
IEnumerable<T> is iterated. Because the sequence reads directly from the live database
reader, it must be iterated before the enclosing using (ResultReader ...) block exits.
Storing the enumerable and iterating it after the reader has been disposed will throw an
InvalidOperationException or ObjectDisposedException.
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