Linq extension to query couchbase lite repository (Linq to Expression)
Writen in .net standard 2.0
The class Linq2CouchbaseLiteExpression contains a unique method named GenerateFromExpression that generate an couchbase lite IExpression.
This generic method works on objects that are class.
First of all, install the plugin by the nuget package.
/// <summary>
/// Entity object
/// </summary>
public class EntityObject
{
public string Id { get; set; }
public string Name { get; set; }
public int Value { get; set; }
public bool IsHuman { get; set; }
}
// Generate IExpression from a Lambda expression :
var resultFilter = Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == "test");
This call will generate a Couchbase lite expression like this :
// Generate manually the IExpression :
Couchbase.Lite.Query.Expression.Property("Name").EqualTo(Couchbase.Lite.Query.Expression.String("test"))
These operations are now supported
Function | Example |
---|---|
> | (e) => e.Value > 10 |
< | (e) => e.Value < 10 |
>= | (e) => e.Value >= 10 |
>= | (e) => e.Value <= 10 |
== | (e) => e.Name == "test" |
!= | (e) => e.Name != "test" |
! | (e) => !e.IsHuman |
.Equals | (e) => e.Name.Equals("test") |
string.IsNullOrEmpty | (e) => string.IsNullOrEmpty(e.Name) |
You can also combine conditions :
Function | Example |
---|---|
|| | (e) => e.Name == "test" || e.Name == "test 2" |
&& | (e) => e.Value > 10 && e.Name == "test 2" |
The expression must respect some specific rules :
- You can set field object at left or at right of the operation :
// Valid :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == "test");
// Or :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => "test" = e.Name );
- You can use call static method withtout parameters only :
// Valid :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == CallToStaticMethod());
// Invalid :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == CallToStaticMethod("Parameter"));
// Invalid :
var customObject = new CustomObject();
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == customObject.NonPublicMethod());
// Invalid :
var customObject = new CustomObject();
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name == customObject.NonPublicMethodWithParameters("test"));
- Only the methods are now supported : .Equals() or string.IsNullOrEmpty()
// Valid :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => e.Name.Equals("test"));
// Valid :
Linq2CouchbaseLiteExpression.GenerateFromExpression<EntityObject>((e) => string.IsNullOrEmpty(e.Name);
You want more ? Feel free to create an issue or contribute by adding new functionnalities by forking the project and create a pull request.
And if you like this project, don't forget to star it !
You can also support me with a coffee :