Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
janaks09 committed Jan 20, 2018
2 parents 6ce255e + efa8d9f commit 6bf8db9
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# DynamiceExpressionBuilder
C# expression builder at run-time.
## Overview
Simple C# expression builder at run-time.
Currently supports these operations:
```csharp
public enum Operation
{
Equals,
NotEquals,
GreaterThan,
LessThan,
GreaterThanOrEqual,
LessThanOrEqual,
Contains,
ContainsWithToLower,
StartsWith,
EndsWith,
StringEquals
}
```

## Implementation
Steps:
+ Build expression input list
```csharp
public static IList<ExpressionInput> GetExpressionInputList()
{
return new List<ExpressionInput>
{
new ExpressionInput
{
Operand = QueryOperand.And, //First Item does not matter And or OR
Operation = Operation.Contains,
PropertyName = "Name",
Value = "Jack"
},
new ExpressionInput
{
Operand = QueryOperand.And,
Operation = Operation.StringEquals, //Operation.Equals
PropertyName = "State",
Value = "FL"
},
new ExpressionInput
{
Operand = QueryOperand.Or,
Operation = Operation.NotEquals,
PropertyName = "CrimeRecord",
Value = false
},
new ExpressionInput
{
Operand = QueryOperand.And,
Operation = Operation.GreaterThanOrEqual,
PropertyName = "AnnualIncome",
Value = (double)500000 //Value need to be parsed to Expression's object (T) type. Here T is of Citizen type and AnnualIncome is of double type.
}
};
}
```
+ Passed to ExpressionBuilder
```csharp
var expressionList = ExpressionInputGenerator.GetExpressionInputList();

var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList); //Passing to expression builder. Here we are getting expression of Type Citizen
Console.WriteLine($"Final Expression = {expression.ToString()}");
```
Output: `Final Expression = t => (((t.Name.Contains("Jack") AndAlso (Compare(t.State, "FL", OrdinalIgnoreCase) == 0)) Or (t.CrimeRecord != False)) AndAlso (t.AnnualIncome >= 500000))`
+ Using Final expression
```csharp
var citizenRecords = CitizenRecordGenerator.GetCitizenRecordList();
var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList)

var filterCitizens = citizenRecords.Where(expression.Compile()); //Expression Implementation
Console.WriteLine($"\nFilter records: {filterCitizens.Count()}");
```

## Conclusion

Above code are from example console app which is inside project.


0 comments on commit 6bf8db9

Please sign in to comment.