Skip to content

3.1. Working with basic type parameters

Vedran Bilopavlović edited this page Mar 26, 2021 · 2 revisions

Working with the basic, built-in types (int, string, etc) built-in type parameters

Important notes when mapping built-in types:

  1. Mappings are always by position, not by name.
  1. All database nulls are automatically converted to null where applicable. Use nullable types to avoid errors. For example DateTime? or bool?.

Get a single value example

using System.Linq;

// ...
var name = connection.Read<string>("SELECT CustomerName FROM Customers where CustomerID = 1;").Single();
Console.WriteLine(name); // Alfreds Futterkiste 

Try it yourself

Get two values example

using System.Linq;

// ...
var (name, price) = connection.Read<string, decimal>("SELECT ProductName, Price FROM Products where ProductID = 1;").Single();
Console.WriteLine($"{name.Trim()} has price {price}"); // Chais has price 18.0000

Try it yourself

Iterate three values example

foreach(var (name, unit, price) in connection.Read<string, string, decimal>("SELECT ProductName, Unit, Price FROM Products"))
{
    Console.WriteLine($"{name.Trim()} has price {price} per {unit}");
}

Try it yourself

Build dictionary example

using System.Linq;

//...
var dict = connection.Read<int, string>("SELECT ProductID, ProductName FROM Products").ToDictionary(t => t.Item1, t => t.Item2);
foreach(var (key, value) in dict)
{
    Console.WriteLine($"{key} {value.Trim()}");
}

Try it yourself

For more information and options when working with parameters see also

For more information and options when working with parameters see also

See also

Clone this wiki locally