Method BindResults
- Namespace
- YndigoBlue.Velocity.Engine
- Assembly
- YndigoBlue.Velocity.dll
BindResults<T>(BindingType)
Binds query results to a typed list using the specified binding strategy.
public IList<T> BindResults<T>(BindingType bindingType = BindingType.DirectMap) where T : new()
Parameters
bindingTypeBindingTypeThe binding strategy to use (DirectMap, SnakeCase, CamelCase, or Attribute). Defaults to DirectMap.
Returns
- IList<T>
A list 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 Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int StockQuantity { get; set; }
}
var conn = new MySqlDatasourceConnection
{
Hostname = "localhost",
Database = "inventory",
Username = "root",
Password = "password"
};
using (var manager = new Manager(conn))
{
var schema = manager.LoadSchema("inventory");
var productsTable = schema["products"];
var query = new Query()
.Select([productsTable["id"], productsTable["name"], productsTable["price"], productsTable["stock_quantity"]])
.From(productsTable);
ResultSet results = manager.ExecuteResultSet(query);
// Bind to typed objects using SnakeCase (stock_quantity -> StockQuantity)
IList<Product> products = results.BindResults<Product>(BindingType.SnakeCase);
// Can now use LINQ and iterate multiple times
var lowStock = products.Where(p => p.StockQuantity < 10).OrderBy(p => p.Name);
var totalValue = products.Sum(p => p.Price * p.StockQuantity);
Console.WriteLine($"Total inventory value: {totalValue:C}");
results.Dispose();
}
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