Table of Contents

Class Result

Namespace
YndigoBlue.Velocity.Model
Assembly
YndigoBlue.Velocity.dll

Represents a single row returned from a SELECT query.

public class Result : Datum
Inheritance
Result
Extension Methods

Remarks

Result is the primary object for working with query data. Each Result instance represents one row from the database, with strongly-typed access to column values through inherited Datum methods.

Results are returned as collections from Retrieve(Query) and can be:

  • Accessed field-by-field using GetField* methods
  • Bound to C# objects using BindResults<T>(BindingType)
  • Checked for NULL values using IsNull methods

Examples

Basic query and result access:

using (Manager manager = new Manager(connection))
{
    Schema schema = manager.LoadSchema("my_schema");
    Table usersTable = schema["users"];

    var query = new Query()
        .Select([ usersTable["id"], usersTable["username"], usersTable["email"], usersTable["created_at"] ])
        .From(usersTable);

    using (ResultSet results = manager.Retrieve(query))
    {
        foreach (Result result in results)
        {
            int id = result.GetFieldInt("id");
            string username = result.GetFieldString("username");
            string email = result.GetFieldString("email");
            DateTime createdAt = result.GetFieldDateTime("created_at");

            Console.WriteLine($"{id}: {username} ({email}) - {createdAt}");
        }
    }
}

Handling NULL values:

var query = new Query()
    .Select([ usersTable["id"], usersTable["last_login"] ])
    .From(usersTable);

using (ResultSet results = manager.Retrieve(query))
{
    foreach (Result result in results)
    {
        int id = result.GetFieldInt("id");

        if (result.IsNull(usersTable["last_login"]))
        {
            Console.WriteLine($"User {id} has never logged in");
        }
        else
        {
            DateTime lastLogin = result.GetFieldDateTime("last_login");
            Console.WriteLine($"User {id} last logged in: {lastLogin}");
        }
    }
}

Binding results to C# objects:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Email { get; set; }
    public DateTime CreatedAt { get; set; }
}

var query = new Query()
    .Select([ usersTable["id"], usersTable["username"], usersTable["email"], usersTable["created_at"] ])
    .From(usersTable);

using (ResultSet results = manager.Retrieve(query))
{
    // Automatically bind results to User objects
    IList<User> users = results.BindResults<User>(BindingType.SnakeCase);

    foreach (User user in users)
    {
        Console.WriteLine($"{user.Id}: {user.Username}");
    }
}

Working with calculated fields:

Expression totalExpression = new Expression("total");
totalExpression.Add(ordersTable["quantity"]);
totalExpression.Add(new ArithmeticOperator(ArithmeticType.Multiply));
totalExpression.Add(ordersTable["price"]);

var query = new Query()
    .Select([ ordersTable["id"], totalExpression ])
    .From(ordersTable);

using (ResultSet results = manager.Retrieve(query))
{
    foreach (Result result in results)
    {
        int id = result.GetFieldInt("id");
        decimal total = result.GetFieldDecimal("total");
        Console.WriteLine($"Order {id}: ${total}");
    }
}

Constructors

Result(OrderedDictionary<string, ISelectItem>)

Initializes a new Result with the ordered set of select items that describe the columns in this row.

Properties

FieldCount

Gets the number of columns in this result row.

Fields

Gets the ordered collection of select items (columns) that make up this result row.

Methods

GetFieldString(string)

Gets the value of the specified field as an untyped string. Checks char and varchar columns as fallbacks if no string column is found.

GetFieldValue(string, DataType)

Gets the value of a named field, returned as the CLR type that corresponds to dataType.

IsNull(ISelectItem)

Determines if the select item is null within this datum either as a result of not being in the datum or being in the datum and being set to null.

SetFieldString(string, string)

Sets a field value using the generic string data type. Used internally when reading database results that return an untyped string column.