Skip to content

Commit d202efc

Browse files
Add eval docs
Add eval docs
1 parent 203a18a commit d202efc

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Eval.Compile
2+
3+
## Description
4+
You can compile C# expression at runtime using the [Eval-Expression.NET](http://eval-expression.net/) library.
5+
6+
You can download it here: [Download](http://eval-expression.net/download)
7+
8+
> The Eval-Expression.NET library can be activated with the Eval-SQL.NET license.
9+
10+
## Compile and return a strongly typed delegate
11+
You can return the delegate as a strongly typed function or action:
12+
13+
- Eval.Compile<TDelegate>(string code)
14+
- Eval.Compile<TDelegate>(string code, IEnumerable<string> parameterNames)
15+
- Eval.Compile<TDelegate>(string code, params string[] parameterNames)
16+
17+
### Example
18+
```csharp
19+
// Delegate Func
20+
var compiled = Eval.Compile<Func<int, int, int>>("{0} + {1}");
21+
int result = compiled(1, 2);
22+
23+
// Delegate Action
24+
var compiled = Eval.Compile<Action<int, int>>("{0} + {1}");
25+
compiled(1, 2);
26+
27+
// Named Parameter
28+
var compiled = Eval.Compile<Func<int, int, int>>("X + Y", "X", "Y");
29+
int result = compiled(1, 2);
30+
```
31+
{% include component-try-it.html href='https://dotnetfiddle.net/MBHlX8' %}
32+
33+
## Compile and return a delegate
34+
You can return the delegate as a generic delegate:
35+
36+
- Eval.Compile(string code): Func&lt;object&gt;
37+
- Eval.Compile(string code, Type type1): Func&lt;object, object&gt;
38+
- Eval.Compile(string code, Type type1, ... , Type type9): Func&lt;object, ... , object, object&gt;
39+
- Eval.Compile(string code, IEnumerable&lt;Type&gt; types): Func&lt;IEnumerable, object&gt;
40+
- Eval.Compile(string code, params Type[] types): Func&lt;IEnumerable, object&gt;
41+
- Eval.Compile(string code, IDictionary&lt;string, Type&gt; nameTypes): Func&lt;IDictionary, object&gt;
42+
43+
### Example
44+
```csharp
45+
// Overload: Up to 9 parameters can be used
46+
var compiled = Eval.Compile("{0} + {1}", typeof(int), typeof(int));
47+
object result = compiled(1, 2);
48+
49+
// Overload: params Type[]
50+
var values = new List<int>() {1, 2};
51+
var types = values.Select(x => x.GetType()).ToArray();
52+
53+
var compiled = Eval.Compile("{0} + {1}", types);
54+
var result = compiled(values);
55+
56+
// Overload: IDictionary<string, Type>
57+
var values = new Dictionary<string, object> { {"X", 1}, {"Y", 2} };
58+
var types = values.ToDictionary(x => x.Key, x => x.Value.GetType());
59+
60+
var compiled = Eval.Compile("X + Y", types);
61+
var result = compiled(values);
62+
```
63+
{% include component-try-it.html href='https://dotnetfiddle.net/870F71' %}
64+
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Eval.Execute
2+
3+
## Description
4+
You can execute C# expression at runtime using the [Eval-Expression.NET](http://eval-expression.net/) library.
5+
6+
You can download it here: [Download](http://eval-expression.net/download)
7+
8+
> The Eval-Expression.NET library can be activated with the Eval-SQL.NET license.
9+
10+
You can specify parameter value to use in the expression from various way:
11+
12+
- Anonymous Type
13+
- Argument Position
14+
- Class Member
15+
- Dictionary
16+
17+
Under the hood, the fist time an expression is executed, it's getting compiled and the delegate is stored in the memory before being returned and executed. All future call from the same expression will retrieve the delegate from the memory to optimize the performance.
18+
19+
Even with this optimization, if you have to evaluate multiple times the same expression, by example in a for loop, we highly recommend you to use directly the delegate returning from the Compile method instead.
20+
21+
## Execute and return a strongly typed result
22+
You can return the result as a strongly typed type:
23+
24+
- Eval.Execute&lt;TResult&gt;(string code)
25+
- Eval.Execute&lt;TResult&gt;(string code, object parameters)
26+
- Eval.Execute&lt;TResult&gt;(string code, params object[] parameters)
27+
28+
### Example
29+
30+
```csharp
31+
// Parameter: Anonymous Type
32+
int result = Eval.Execute<int>("X + Y", new { X = 1, Y = 2} );
33+
34+
// Parameter: Argument Position
35+
int result = Eval.Execute<int>("{0} + {1}", 1, 2);
36+
37+
// Parameter: Class Member
38+
dynamic expandoObject = new ExpandoObject();
39+
expandoObject.X = 1;
40+
expandoObject.Y = 2;
41+
int result = Eval.Execute<int>("X + Y", expandoObject);
42+
43+
// Parameter: Dictionary Key
44+
var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} };
45+
int result = Eval.Execute<int>("X + Y", values);
46+
```
47+
{% include component-try-it.html href='https://dotnetfiddle.net/W9TwcP' %}
48+
49+
## Execute and return an object result
50+
You can return the result as an object:
51+
52+
- Eval.Execute(string code)
53+
- Eval.Execute(string code, object parameters)
54+
- Eval.Execute(string code, params object[] parameters)
55+
56+
```csharp
57+
// Parameter: Anonymous Type
58+
object result = Eval.Execute("X + Y", new { X = 1, Y = 2} );
59+
60+
// Parameter: Argument Position
61+
object result = Eval.Execute("{0} + {1}", 1, 2);
62+
63+
// Parameter: Class Member
64+
dynamic expandoObject = new ExpandoObject();
65+
expandoObject.X = 1;
66+
expandoObject.Y = 2;
67+
object result = Eval.Execute("X + Y", expandoObject);
68+
69+
// Parameter: Dictionary Key
70+
var values = new Dictionary<string, object>() { {"X", 1}, {"Y", 2} };
71+
object result = Eval.Execute("X + Y", values);
72+
```
73+
{% include component-try-it.html href='https://dotnetfiddle.net/8mtLH8' %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# LINQ Dynamic
2+
3+
## Description
4+
You can execute query dynamically through the [Eval-Expression.NET](http://eval-expression.net/) library.
5+
6+
You can download it here: [Download](http://eval-expression.net/download)
7+
8+
> The Eval-Expression.NET library can be activated with the Eval-SQL.NET license.
9+
10+
## LINQ Dynamic - Predicate
11+
You can use any LINQ method that support predicate with a dynamic C# expression :
12+
13+
- Deferred
14+
- SkipWhile
15+
- TakeWhile
16+
- Where
17+
- Immediate
18+
- All
19+
- Any
20+
- Count
21+
- First
22+
- FirstOrDefault
23+
- Last
24+
- LastOrDefault
25+
- LongCount
26+
- Single
27+
- SingleOrDefault
28+
29+
### Example
30+
```csharp
31+
var list = new List<int>() { 1, 2, 3, 4, 5 };
32+
33+
var list2 = list.Where(x => "x > 2");
34+
var list3 = list.Where(x => "x > X", new { X = 2 }); // with parameter
35+
```
36+
{% include component-try-it.html href='https://dotnetfiddle.net/S42mkU' %}
37+
38+
## LINQ Dynamic - Ordering and Selecting
39+
You can use any ordering and selecting method with a dynamic C# expression:
40+
41+
- OrderByDescendingDynamic
42+
- OrderByDynamic
43+
- SelectDynamic
44+
- SelectMany
45+
- ThenByDescendingDynamic
46+
- ThenByDynamic
47+
48+
> The **"Dynamic"** suffix is required for not overriding the default behavior (ordering or selecting by a string is valid).
49+
50+
### Example
51+
```csharp
52+
var list = new List<int>() { 5, 2, 4, 1, 3 };
53+
54+
// SelectDynamic
55+
var list2 = list.SelectDynamic(x => "new { y = x + 1 }");
56+
var list3 = list.SelectDynamic(x => "new { y = x + 1 }", new { y = 1 });
57+
58+
// OrderByDynamic
59+
var list4 = list.OrderByDynamic(x => "x + 1");
60+
var list5 = list.OrderByDynamic(x => "x + Y", new { Y = 1 });
61+
```
62+
63+
## LINQ Dynamic - Execute
64+
You can push the LINQ Dynamic experience further by using the Execute method and chaining anything else:
65+
66+
- Execute
67+
- Execute&lt;TResult&gt;
68+
69+
### Example
70+
```csharp
71+
var list = new List<int>() { 1, 2, 3, 4, 5 };
72+
73+
var list2 = list.Execute<List<int>>("Where(x => x > 2).OrderBy(x => x).ToList()");
74+
var list3 = list.Execute<List<int>>("Where(x => x > y).OrderBy(x => x).ToList()", new { y = 2 });
75+
```

0 commit comments

Comments
 (0)