Get Started

ServiceQuery is available as a NuGet package. You can download and install it using the .NET CLI or Visual Studio Package Manager Console.

Install NuGet Package:

ServiceQuery

Simple Database Access or In-Memory Lists


    using ServiceQuery;

    // Get your Database Table/Collection as an IQueryable object
    var queryable = databaseContext.ExampleTable.AsQueryable();

    // Use in-memory lists as well
    var queryableList = new List()
    {
        new ExampleTable(){
            Id = 1,
            FirstName = "John"
        }
    }.AsQueryable();

    // Build a request using the ServiceQueryRequestBuilder object
    var request = new ServiceQueryRequestBuilder().Build();

    // Execute the request against the database and get the results
    var response = request.Execute(queryable);

    // Now supporting async usage
    var responseAsync = await request.ExecuteAsync(queryable);

        

Use in a REST API


    using ServiceQuery;

    // For sync usage
    [HttpPost]
    [Route("ExampleServiceQuery")]
    public ServiceQueryResponse ExampleServiceQuery(ServiceQueryRequest request)
    {
        var queryable = databaseContext.ExampleTable.AsQueryable();
        return request.Execute(queryable);
    }

    // For async usage
    [HttpPost]
    [Route("ExampleServiceQueryAsync")]
    public async Task<ServiceQueryResponse<ExampleTable>> ExampleServiceQueryAsync(ServiceQueryRequest request)
    {
        var queryable = databaseContext.ExampleTable.AsQueryable();
        return await request.ExecuteAsync(queryable);
    }
        

Use in JavaScript/jQuery

Include servicequery.js in your project.


    
    
        

Building Queries

The following are some examples to help you build queries. Include servicequery.js in your project and use the same code in .NET or javascript below!


    // Default
    var request = new ServiceQueryRequestBuilder().Build();    

    // Paging records
    request = new ServiceQueryRequestBuilder().Paging(1, 1000, false).Build(); // Same as default

    // Paging records with a total count
    request = new ServiceQueryRequestBuilder().Paging(1, 1000, true).Build(); // Page number 1, Page size 1000, include count
    request = new ServiceQueryRequestBuilder().IncludeCount().Build(); // Same as above line   

    // Aggregates do not return records, only an aggregate property
    request = new ServiceQueryRequestBuilder().Count().Build();
    request = new ServiceQueryRequestBuilder().Sum("Price").Build();    
    request = new ServiceQueryRequestBuilder().Average("Price").Build();
    request = new ServiceQueryRequestBuilder().Minimum("Price").Build();
    request = new ServiceQueryRequestBuilder().Maximum("Price").Build();

    // Selecting specific properties to reduce response sizes
    request = new ServiceQueryRequestBuilder().Select("Id","FirstName","LastName").Build();

    // Building conditional AND/OR expressions
    request = new ServiceQueryRequestBuilder().IsEqual("Id","1").And().StartsWith("FirstName", "John").Build();
    request = new ServiceQueryRequestBuilder().Between("Id","1", "5").Or().Contains("LastName", "Smith").Build();

    // Grouped expressions (including nesting), use Begin() and End()
    request = new ServiceQueryRequestBuilder()
        .Begin()
            .IsGreaterThan("Id","100")
            .And()
            .IsInSet("Status", "Created", "Open", "InProcess")
            .Or()
            .IsNull("EmployeeId")
        .End()
        .Or()
        .Begin()
            .IsLessThanOrEqual("BirthDate","1/1/2000")
            .And()
            .IsNotNull("CloseDate")
        .End()
        .Build();

    // Sorting
    request = new ServiceQueryRequestBuilder().SortAsc("FirstName").Build();
    request = new ServiceQueryRequestBuilder().SortDesc("LastName").Build();
         
        

Asynchronous Support

Starting with v2.2.0, we have included async support. Since async support is delegated to provider-specific implementations, we must incorporate provider packages to support them. The following are our supported list:

  • ServiceQuery.AzureDataTables - using ExecuteAsync()
    Support for Azure Data Tables (Storage Account). It also provides a solution for unsupported features, such as string comparisons, aggregates functions and sorting.
  • ServiceQuery.EntityFrameworkCore - using ExecuteAsync()
    Supporting EFC based database engines. Note: This package targets EFC 9 for .NET runtime 8.
  • ServiceQuery.EntityFrameworkCore8 - using ExecuteAsync()
    Supporting EFC based database engines. Note: This package only targets EFC 8 for .NET runtime 8.
  • ServiceQuery.MongoDb - using MongoDbExecuteAsync()
    Support for MongoDB.