Skip to content

Commit 6bf8db9

Browse files
committed
2 parents 6ce255e + efa8d9f commit 6bf8db9

1 file changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# DynamiceExpressionBuilder
2-
C# expression builder at run-time.
1+
## Overview
2+
Simple C# expression builder at run-time.
3+
Currently supports these operations:
4+
```csharp
5+
public enum Operation
6+
{
7+
Equals,
8+
NotEquals,
9+
GreaterThan,
10+
LessThan,
11+
GreaterThanOrEqual,
12+
LessThanOrEqual,
13+
Contains,
14+
ContainsWithToLower,
15+
StartsWith,
16+
EndsWith,
17+
StringEquals
18+
}
19+
```
20+
21+
## Implementation
22+
Steps:
23+
+ Build expression input list
24+
```csharp
25+
public static IList<ExpressionInput> GetExpressionInputList()
26+
{
27+
return new List<ExpressionInput>
28+
{
29+
new ExpressionInput
30+
{
31+
Operand = QueryOperand.And, //First Item does not matter And or OR
32+
Operation = Operation.Contains,
33+
PropertyName = "Name",
34+
Value = "Jack"
35+
},
36+
new ExpressionInput
37+
{
38+
Operand = QueryOperand.And,
39+
Operation = Operation.StringEquals, //Operation.Equals
40+
PropertyName = "State",
41+
Value = "FL"
42+
},
43+
new ExpressionInput
44+
{
45+
Operand = QueryOperand.Or,
46+
Operation = Operation.NotEquals,
47+
PropertyName = "CrimeRecord",
48+
Value = false
49+
},
50+
new ExpressionInput
51+
{
52+
Operand = QueryOperand.And,
53+
Operation = Operation.GreaterThanOrEqual,
54+
PropertyName = "AnnualIncome",
55+
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.
56+
}
57+
};
58+
}
59+
```
60+
+ Passed to ExpressionBuilder
61+
```csharp
62+
var expressionList = ExpressionInputGenerator.GetExpressionInputList();
63+
64+
var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList); //Passing to expression builder. Here we are getting expression of Type Citizen
65+
Console.WriteLine($"Final Expression = {expression.ToString()}");
66+
```
67+
Output: `Final Expression = t => (((t.Name.Contains("Jack") AndAlso (Compare(t.State, "FL", OrdinalIgnoreCase) == 0)) Or (t.CrimeRecord != False)) AndAlso (t.AnnualIncome >= 500000))`
68+
+ Using Final expression
69+
```csharp
70+
var citizenRecords = CitizenRecordGenerator.GetCitizenRecordList();
71+
var expression = DynamicExpressionBuilder.ExpressionBuilder.GetExpression<Citizen>(expressionList)
72+
73+
var filterCitizens = citizenRecords.Where(expression.Compile()); //Expression Implementation
74+
Console.WriteLine($"\nFilter records: {filterCitizens.Count()}");
75+
```
76+
77+
## Conclusion
78+
79+
Above code are from example console app which is inside project.
80+
81+

0 commit comments

Comments
 (0)