Skip to main content.

Basics

Executing a Query

In order to execute a query, we need two (2) things:

  • A ServiceQueryRequest object - See Query Building
  • An IQueryable interface to our data store

Get the IQueryable interface to the data source

Most database engines provide an IQueryable interface so you can access its table data. You can also use ServiceQuery with just a list of objects.

Example 1 Get IQueryable for a database table
var queryable = databaseContext.ExampleTable.AsQueryable();
Example 2 Get IQueryable from a list of example objects
List<Example> examples = new List<Example>(){
new Example(){ Id = 1, Name = "a" },
new Example(){ Id = 2, Name = "b" },
};
var queryable = examples.AsQueryable();

Execute the Query

After obtaining the ServiceQueryRequest object and IQueryable object, call the Execute extension method on the ServiceQueryRequest object. This will return a ServiceQueryResponse object.

var request = ServiceQueryRequestBuilder.New().Build();
var queryable = databaseContext.ExampleTable.AsQueryable();
var response = request.Execute(queryable);

Inspect the ServiceQueryResponse object

The ServiceQueryResponse object contains three (3) properties:

  • List - List of objects returned from the query
  • Count - If IncludeCount was requested, this is a count of the records
  • Aggregate - If the ServiceQuery contained an Aggregate operation, this contains the result
var request = ServiceQueryRequestBuilder.New().Build();
var queryable = databaseContext.ExampleTable.AsQueryable();
var response = request.Execute(queryable);

List<ExampleTable> list = response.List;
long count = response.Count;
double? aggregate = response.Aggregate;