Skip to content

Commit

Permalink
LambdaExpressionExtensions sample notebook (#191)
Browse files Browse the repository at this point in the history
* 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
ElisendaGascon and jesshill99 authored Oct 19, 2022
1 parent d91090f commit 8c28851
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Corvus.Extensions.Samples/LambdaExpressionExtensionsSample.dib
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

0 comments on commit 8c28851

Please sign in to comment.