-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LambdaExpressionExtensions sample notebook (#191)
* LambdaExpressionExtensions sample notebook * Addressed PR comment fixed typo in title * Add "How to use .NET Interactive notebooks" section * Remove how to use .NET interactive notebooks * Adding examples * Remove comment in code cell Co-authored-by: Jessica <[email protected]>
- Loading branch information
1 parent
d91090f
commit 8c28851
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
Corvus.Extensions.Samples/LambdaExpressionExtensionsSample.dib
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!markdown | ||
|
||
## Lambda Expression Extensions - Sample Notebook | ||
|
||
#!markdown | ||
|
||
First, add a reference to the `Corvus.Extensions` NuGet package. | ||
|
||
#!csharp | ||
|
||
#r "nuget: Corvus.Extensions, 1.1.4" | ||
|
||
using Corvus.Extensions; | ||
using System.Linq.Expressions; | ||
|
||
#!markdown | ||
|
||
### ExtractPropertyName() | ||
|
||
#!markdown | ||
|
||
Extracts a property name from a lambda expression, throwing if that expression is not a `MemberExpression`. | ||
|
||
#!markdown | ||
|
||
This can be useful in code that bridges between .NET types and external storage. In particular, if a library wants to allow C# expressions for writing queries against storage, it can be neccessary to find out which properties a C# expression uses, to be able to work out what that maps to in the external storage. | ||
|
||
#!csharp | ||
|
||
Expression<Func<string, int>> getter = x => x.Length; | ||
|
||
#!csharp | ||
|
||
getter.ExtractPropertyName() | ||
|
||
#!markdown | ||
|
||
### GetMemberExpression() | ||
|
||
#!markdown | ||
|
||
Extracts a `MemberExpression` from a `LambdaExpression`, throwing if the body is not a `MemberExpression`. | ||
|
||
#!markdown | ||
|
||
This allows a more direct expression of the expectation that an expression has this particular form. It allows us to avoid cluttering up the code with exception throwing, which can improve readability. | ||
|
||
#!csharp | ||
|
||
getter.GetMemberExpression().Expression |