Skip to content

Commit

Permalink
Reorder samples based on article order; added NodeType Value Name sample
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Aug 26, 2019
1 parent 5298a46 commit c30891b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 60 deletions.
71 changes: 37 additions & 34 deletions CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,31 @@ namespace CSharp {
class Program {
static void Main(string[] args) {
RunSample(() => Figure2_ExpressionsAsObjects());
RunSample(() => Figure3_NodetypeValueName());
RunSample(() => Figure5_BlocksAssignmentsStatements());
RunSample(() => Figure6_QueryableWhere());
RunSample(() => Inline_GetMethod());
RunSample(() => Figure7_SimpleODataClient());
RunSample(() => Inline_GetMethod());
}

/**
* <summary>Shows the objects in the expression tree for n + 42 == 27, using object and collection initializers</summary>
*/
public static void Figure2_ExpressionsAsObjects() {
var n = Parameter(typeof(int), "n");
var expr = Equal(
private static readonly BinaryExpression simpleExpression =
Equal(
Add(
n,
Parameter(typeof(int), "n"),
Constant(42)
),
Constant(27)
);
Console.WriteLine(expr.ToString("Object notation"));
}

/**
* <summary>Shows the objects in the expression tree for n + 42 == 27, using object and collection initializers</summary>
*/
public static void Figure2_ExpressionsAsObjects() => Console.WriteLine(simpleExpression.ToString("Object notation"));

/**
* <summary>Shows the NodeType, Value and Name properties for n + 42 == 27; useful for visualizing the expression tree's structure</summary>
*/
public static void Figure3_NodetypeValueName() => Console.WriteLine(simpleExpression.ToString("Textual tree"));

/**
* <summary>
Expand Down Expand Up @@ -103,6 +108,28 @@ public static void Figure6_QueryableWhere() {
Console.WriteLine(expressionAfterWhere.ToString("Textual tree"));
}

/**
* <summary>Constructing OData web requests using expression trees and the Simple.OData.Client library</summary>
*/
public static void Figure7_SimpleODataClient() {
var client = new ODataClient("https://services.odata.org/v4/TripPinServiceRW/");

var command = client.For<Person>()
.Filter(x => x.Trips.Any(y => y.Budget > 3000))
.Top(2)
.Select(x => new { x.FirstName, x.LastName });

var commandText = Task.Run(() => command.GetCommandTextAsync()).Result;
// Prints the generated relative URL
Console.WriteLine(commandText);
Console.WriteLine();

var people = Task.Run(() => command.FindEntriesAsync()).Result;
foreach (var p in people) {
p.Write();
}
}

/**
* <summary>Use compiled expressions to target a specific MethodInfo, instead of reflection</summary>
*/
Expand All @@ -127,29 +154,5 @@ public static void Inline_GetMethod() {
}).MakeGenericMethod(typeof(Person))
);
}

/**
* <summary>Constructing OData web requests using expression trees and the Simple.OData.Client library</summary>
*/
public static void Figure7_SimpleODataClient() {
var client = new ODataClient("https://services.odata.org/v4/TripPinServiceRW/");

var command = client.For<Person>()
.Filter(x => x.Trips.Any(y => y.Budget > 3000))
.Top(2)
.Select(x => new { x.FirstName, x.LastName });

var commandText = Task.Run(() => command.GetCommandTextAsync()).Result;
// Prints the generated relative URL
Console.WriteLine(commandText);
Console.WriteLine();

var people = Task.Run(() => command.FindEntriesAsync()).Result;
foreach (var p in people) {
p.Write();
}
}


}
}
59 changes: 33 additions & 26 deletions VisualBasic/Program.vb
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ Imports System.Reflection
Module Program
Sub Main(args As String())
RunSample(Sub() Figure2_ExpressionsAsObjects())
RunSample(Sub() Figure3_NodetypeValueName())
RunSample(Sub() Figure5_BlocksAssignmentsStatements())
RunSample(Sub() Figure6_QueryableWhere())
RunSample(Sub() Inline_GetMethod())
RunSample(Sub() Figure7_SimpleODataClient())
RunSample(Sub() Inline_GetMethod())
End Sub

''' <summary>Shows the objects in the expression tree for n + 42 == 27, using object and collection initializers</summary>
Sub Figure2_ExpressionsAsObjects()
Dim n = Parameter(GetType(Integer), "n")
Dim expr = Equal(
Private ReadOnly simpleExpression As BinaryExpression =
Equal(
Add(
n,
Parameter(GetType(Integer), "n"),
Constant(42)
),
Constant(27)
)
WriteLine(expr.ToString("Object notation", "Visual Basic"))

''' <summary>Shows the objects in the expression tree for n + 42 == 27, using object and collection initializers</summary>
Sub Figure2_ExpressionsAsObjects()
WriteLine(simpleExpression.ToString("Object notation", "Visual Basic"))
End Sub

''' <summary>Shows the NodeType, Value and Name properties for n + 42 == 27; useful for visualizing the expression tree's structure</summary>
Sub Figure3_NodetypeValueName()
WriteLine(simpleExpression.ToString("Textual tree", "Visual Basic"))
End Sub

''' <summary>
Expand Down Expand Up @@ -79,6 +86,25 @@ Module Program
WriteLine(expressionAfterWhere.ToString("Textual tree", "Visual Basic"))
End Sub

''' <summary>Constructing OData web requests using expression trees and the Simple.OData.Client library</summary>
Sub Figure7_SimpleODataClient()
Dim client = New ODataClient("https://services.odata.org/v4/TripPinServiceRW/")

Dim command = client.For(Of Person).
Filter(Function(x) x.Trips.Any(Function(y) y.Budget > 3000)).
Top(2).
Select(Function(x) New With {x.LastName, x.FirstName})

Dim commandText = Task.Run(Function() command.GetCommandTextAsync()).Result
WriteLine(commandText)
WriteLine()

Dim people = Task.Run(Function() command.FindEntriesAsync).Result
For Each p In people
p.Write()
Next
End Sub

''' <summary>Use compiled expressions to target a specific MethodInfo, instead of reflection</summary>
Sub Inline_GetMethod()
Dim GetMethod As Func(Of Expression(Of Action), MethodInfo) = Function(expr) TryCast(expr.Body, MethodCallExpression)?.Method
Expand All @@ -103,23 +129,4 @@ Module Program
End Function).MakeGenericMethod(GetType(Person))
)
End Sub

''' <summary>Constructing OData web requests using expression trees and the Simple.OData.Client library</summary>
Sub Figure7_SimpleODataClient()
Dim client = New ODataClient("https://services.odata.org/v4/TripPinServiceRW/")

Dim command = client.For(Of Person).
Filter(Function(x) x.Trips.Any(Function(y) y.Budget > 3000)).
Top(2).
Select(Function(x) New With {x.LastName, x.FirstName})

Dim commandText = Task.Run(Function() command.GetCommandTextAsync()).Result
WriteLine(commandText)
WriteLine()

Dim people = Task.Run(Function() command.FindEntriesAsync).Result
For Each p In people
p.Write()
Next
End Sub
End Module

0 comments on commit c30891b

Please sign in to comment.