-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/SurpassSoftware/DynamicEx…
- Loading branch information
Showing
1 changed file
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|