Dapper - Query

Description

Query method is an extension method that can be called from any object of type IDbConnection. It can execute a query and map the result.

The result can be mapped to:

Parameters

The following table shows the different parameters of a Query method.

NameDescription
sqlThe query to execute.
paramThe query parameters (default = null).
transactionThe transaction to use (default = null).
bufferedTrue to buffer readeing the results of the query (default = true).
commandTimeoutThe command timeout (default = null)
commandTypeThe command type (default = null)

Example - Query Anonymous

The raw SQL query can be executed using the Query method and map the result to a dynamic list.

  1. string sql = "SELECT TOP 10 * FROM OrderDetails";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetail = connection.Query(sql).FirstOrDefault();
  5. FiddleHelper.WriteTable(orderDetail);
  6. }

Try it: .NET Core | .NET Framework

Example - Query Strongly Typed

The raw SQL query can be executed using the Query method and map the result to a strongly typed list.

  1. string sql = "SELECT TOP 10 * FROM OrderDetails";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDetails = connection.Query<OrderDetail>(sql).ToList();
  5. Console.WriteLine(orderDetails.Count);
  6. FiddleHelper.WriteTable(orderDetails);
  7. }

Try it: .NET Core | .NET Framework

Example - Query Multi-Mapping (One to One)

The raw SQL query can be executed using the Query method and map the result to a strongly typed list with a one to one relation.

  1. string sql = "SELECT * FROM Invoice AS A INNER JOIN InvoiceDetail AS B ON A.InvoiceID = B.InvoiceID;";
  2. using (var connection = My.ConnectionFactory())
  3. {
  4. connection.Open();
  5. var invoices = connection.Query<Invoice, InvoiceDetail, Invoice>(
  6. sql,
  7. (invoice, invoiceDetail) =>
  8. {
  9. invoice.InvoiceDetail = invoiceDetail;
  10. return invoice;
  11. },
  12. splitOn: "InvoiceID")
  13. .Distinct()
  14. .ToList();
  15. }

Example - Query Multi-Mapping (One to Many)

The raw SQL query can be executed using the Query method and map the result to a strongly typed list with one-to-many relations.

  1. string sql = "SELECT TOP 10 * FROM Orders AS A INNER JOIN OrderDetails AS B ON A.OrderID = B.OrderID;";
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. var orderDictionary = new Dictionary<int, Order>();
  5. var list = connection.Query<Order, OrderDetail, Order>(
  6. sql,
  7. (order, orderDetail) =>
  8. {
  9. Order orderEntry;
  10. if (!orderDictionary.TryGetValue(order.OrderID, out orderEntry))
  11. {
  12. orderEntry = order;
  13. orderEntry.OrderDetails = new List<OrderDetail>();
  14. orderDictionary.Add(orderEntry.OrderID, orderEntry);
  15. }
  16. orderEntry.OrderDetails.Add(orderDetail);
  17. return orderEntry;
  18. },
  19. splitOn: "OrderID")
  20. .Distinct()
  21. .ToList();
  22. Console.WriteLine(list.Count);
  23. FiddleHelper.WriteTable(list);
  24. FiddleHelper.WriteTable(list.First().OrderDetails);
  25. }

Try it: .NET Core | .NET Framework

Example - Query Multi-Type

The raw SQL query can be executed using the Query method and map the result to a list of different types.

  1. string sql = "SELECT * FROM Invoice;";
  2. using (var connection = My.ConnectionFactory())
  3. {
  4. connection.Open();
  5. var invoices = new List<Invoice>();
  6. using (var reader = connection.ExecuteReader(sql))
  7. {
  8. var storeInvoiceParser = reader.GetRowParser<StoreInvoice>();
  9. var webInvoiceParser = reader.GetRowParser<WebInvoice>();
  10. while (reader.Read())
  11. {
  12. Invoice invoice;
  13. switch ((InvoiceKind) reader.GetInt32(reader.GetOrdinal("Kind")))
  14. {
  15. case InvoiceKind.StoreInvoice:
  16. invoice = storeInvoiceParser(reader);
  17. break;
  18. case InvoiceKind.WebInvoice:
  19. invoice = webInvoiceParser(reader);
  20. break;
  21. default:
  22. throw new Exception(ExceptionMessage.GeneralException);
  23. }
  24. invoices.Add(invoice);
  25. }
  26. }
  27. My.Result.Show(invoices);
  28. }