Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Dynamic queries

Daniel Wertheim edited this page Nov 29, 2012 · 5 revisions

From v11.3.0 you can pass lambdas around as strings and turn them into queries. Install the package SisoDb.Dynamic and then you can do:

using SisoDb.Dynamic; //IMPORTANT! Since extension methods are declared in here

public string[] GetSomeJson(string structureTypeName, string predicateExpression, string orderByExpression)
{
    var structureType = GetStructureType(structureTypeName); //Build according to your rules

    using(var session = Db.BeginSession())
    {
        return session.Query(structureType)
                      .Where(predicateExpression)
                      .OrderBy(orderByExpression)
                      .ToArrayOfJson();
    }
}

This can then be used like this:

var articles = GetSomeJson("MyProject.Models.Article", 
                           "article => article.GroupCode == 300", 
                           "article => article.PLU");
Clone this wiki locally