This is a small litte sample project that shows how to use System.Linq.ExpressionVisitor
to convert expressions in anything you desire.
In my case, I wanted to convert expressions on a Queryable
to be used on a rather strange Rest-Style backend called Lookpback.io. Their specification is partially specified on their Online documentation. But it seems that their format is heavily inspired by SailsJs/Waterline and MongoDb. See sources for more details.
var builder = new LoopbackQueryBuilder<Car>();
var query = builder.Where(car => car.Id = 2);
// Result: { "where": { "id": 2 } }
var query = builder.Where(car => car.Id = 2 && car.Name == "Audi");
// Result: { "where": { "and": [ { "id": 2 }, { "name": "Audi" } ] } }
var query = builder.Where(car => car.Name.Contains("au"));
// Result: { "where": { "name": { "like": "%di%" } } }
var query = builder.Where(car => car.Name.Contains("foo") && car.Name == "bla");
// Result: { "where": { "and": [ { "name": { "like": "%foo%" } }, { "name": "bla" } ] } }
var query = builder.Where(car => car.Name.IsPerfect == true);
// Result: { "where": { "isPerfect": true } }
Please note that the aim is not so fully support all possible combinations
The following operations are supported and covered by tests.
- Equality (
==
): Implemented forstring
,int
andbool
- Contains (
x.Contains()
): Implemented forstring
)
- And (
&&
) forstring
,int
andbool
Equality-Expressions, includingContains()
Both Skip()
and Take()
are implemented and will issue additional query parameters.
var query = builder.Where(car => car.Name.Contains("au")).Skip(10).Take(50);
// Result: { "where": { "name": { "like": "%di%" } }, "skip": 10, "take": 50 }
Any other operations, combinations or parameters are currently not supported.