Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Query Dynamic

Trevor Pilley edited this page Mar 13, 2020 · 5 revisions

Dynamic Queries

MicroLite supports returning dynamic results, this allows you to specify dynamic as the return type and retrieve an object which has only the properties specified in the SqlQuery.

The dynamic keyword can be used with the following methods:

  • ISession.Include.Many<dynamic>();
  • ISession.Include.Single<dynamic>(SqlQuery);
  • ISession.FetchAsync<dynamic>();
  • ISession.PagedAsync<dynamic>();
  • ISession.SingleAsync<dynamic>(SqlQuery);

Example

// Create an ad-hoc query, this could select a number of columns across multiple tables if desired.
var query = new SqlQuery("SELECT Name, DoB FROM Customers");

// Execute the query and return the results.
var results = await session.FetchAsync<dynamic>(query);
 
foreach (var item in results)
{
    // The property names of each dynamic result will match
    // (including case) the column names specified in the query.
    Console.WriteLine(item.Name);
    Console.WriteLine(item.DoB);
}

This is supported in MicroLite 4.0 onwards.

Clone this wiki locally