Giving Clarity to LINQ Queries
by Extending Expressions
Are
Do you use
expressions?
Entity Framework & LINQ
Expression<Func<TModel, bool>>
People.Where(p => p.Title == “Developer”)
Automapper
Expression<Func<TSource, object>>
AutoMapper.Mapper.CreateMap<Post, BlogPostView>()
.ForMember(dest => dest.Heading,
opts => opts.MapFrom(src => src.Title));
HTML Helpers
Expression<Func<TModel, TResult>>
@Html.LabelFor(obj => obj.Prop)
Telerik Data Grid
@(Html.Kendo().Grid<Post>
.Columns(columns => {
columns.Bound(p => p.Title);
columns.Bound(p => p.Author);
columns.Bound(p => p.Pubished);
})
)
Expressions in C#
• Representation of code as data
• Meta-programming
– Analyze, rewrite, and translate code at runtime
Homo-iconicity
Same syntax for executable code as data representation
1 //Executable representation
2 Func<int, int> plusOne = n => n + 1;
3
4 //Data representation
5 Expression<Func<int, int>> plusOne = n => n + 1;
Homo-iconicity
Same syntax for executable code as data representation
1 //Executable representation
2 Func<int, int> plusOne = n => n + 1;
3
4 //Data representation
5 Expression<Func<int, int>> plusOne = n => n + 1;
Expression Factory
1 // new Expression() invalid!
2
3 var x = Expression.Constant(1);
1
constant
Expression Factory
1 // new Expression() invalid!
2
3 var x = Expression.Constant(1);
4 var y = Expression.Constant(1);
1
constant
1
constant
Expression Factory
1 // new Expression() invalid!
2
3 var x = Expression.Constant(1);
4 var y = Expression.Constant(1);
5 var sum = Expression.Add(x,y); +
11
Add
constant constant
Expression Factory
1 // new Expression() invalid!
2
3 var x = Expression.Constant(1);
4 var y = Expression.Constant(1);
5 var sum = Expression.Add(x,y);
6
7 Console.WriteLine(sum);
8 // (1 + 1)
9
10 Console.WriteLine(sum.GetType());
11 // SimpleBinaryExpression
+
11
Add
constant constant
SimpleBinaryExpression
Compile
1 Expression<Func<int, int>> plusOne = n => n + 1;
2 Console.WriteLine(plusOne.ToString());
3 // n => (n + 1)
Compile
1 Expression<Func<int, int>> plusOne = n => n + 1;
2 Console.WriteLine(plusOne.ToString());
3 // n => (n + 1)
4
5 var x = plusOne.Compile();
6 Console.WriteLine(x(1));
7 // 2
Runtime Modification
ExpressionVisitor
• used to traverse or rewrite expression trees
• Abstract class
– Inherit and override
• .Visit(expression)
– Recursively walks the tree
– Returns an Expression
https://msdn.microsoft.com/en-us/library/bb882521(v=vs.90).aspx
Are
OMG He’s
showing docs
in a
presentation
Are
How is this
useful?
Pipeline
Pipes and filters
Filter
Rule Rule
Data
Data
Data
Consumer
Results
Database Filter
Rule Rule
dbContext.Where(rule).Where(rule)
Maintainability & Readability
Refactoring
What’s
going
on here?
1 postRepository.GetAll()
2 .Where(post => post.IsPublished &&
3 post.PostedOn <= today &&
4 (post.PostedOn >= cutoffDate ||
5 post.Author == featuredAuthor &&
6 post.PostedOn >= featuredAuthorCutoffDate));
Let’s start simple
What’s
going on
here?
1 postRepository.GetAll()
2 .Where(post => post.IsPublished && post.PostedOn <= today);
Distinction
The tale of two extension methods
IEnumerable.Where(q => q.Value == true)
Function delegate
Func<T, bool>
IQueryable.Where(q => q.Value == true)
Expression
Expression<Func<T, bool>
LINQ Where Chaining
1 //Both statements produce the same result
2
3 //Concise
4 var query = query.Where(x => x.Value == 1 && x.Name == ”foo”);
5
6 //Configurable
7 var query = query.Where(x => x.Value == 1)
8 .Where(x => x.Name == ”foo”);
SELECT x
FROM table
WHERE (value = 1 AND name = ‘foo’)
LINQ Where Chaining
1 var query = query.Where(x => x.Value == 1);
SELECT x
FROM table
WHERE (value = 1)
=>
x
==
value 1
LINQ Where Chaining
1 var query = query.Where(x => x.Value == 1)
2 .Where(x => x.Name == ”foo”);
SELECT x
FROM table
WHERE (value = 1 AND name = ‘foo’)
&&
=>
x
==
name foo
==
value 1
LINQ Where Chaining
1 var query = query.Where(x => x.Value == 1);
2 query.Where(x => x.Name == ”foo”);
3 query.Where(x => x.IsDeleted == ”true”);
SELECT x
FROM table
WHERE (value = 1 AND name = ‘foo’ AND IsDeleted = 1)
&&
=>
x
==
name foo
==
value 1
&&
==
isDeleted
1
Custom Filters
Extends type
IQueryable<MyType>
Return type
IQueryable<MyType>
Method
.CustomMethodName()
public static IQueryable<Post> ArePublished(this IQueryable<Post> posts)
{
return posts.Where(post => post.IsPublished);
}
1 postRepository.GetAll()
2 .Where(post => post.IsPublished &&
3 post.PostedOn <= today);
Readability?
Before After
What’s
the
intent?
1 postRepository.GetAll()
2 .ArePublished()
3 .PostedOnOrBefore(today);
Now what?
What’s
the
intent?
1 IQueryable<Post> posts = postRepository.GetAll()
2 .Where(post => post.IsPublished && post.PostedOn <= today &&
3 (post.PostedOn >= cutoffDate || post.Author == featuredAuthor &&
4 post.PostedOn >= featuredAuthorCutoffDate));
Lambda as an Expression tree
post => post.PostedOn >= cutoffDate
>=
cutoffDatePostedOn
GreaterThanOrEqual
=>
post
Lambda
parameter
parameter constant
Body
Lambda as an Expression tree
post => post.PostedOn >= cutoffDate
>=
cutoffDatePostedOn
GreaterThanOrEqual
=>
post
Lambda
parameter
parameter constant
Body
Lambda as an Expression tree
post => post.PostedOn >= cutoffDate
>=
cutoffDatePostedOn
GreaterThanOrEqual
=>
post
Lambda
parameter
parameter constant
Body
Combine Like Expressions?
>=
=>
post
Body
==
=>
post
Body
AND / OR?
CombineLambdas
>=
constantPost.Property
=>
post
Body
==
constantPost.Property
=>
post
Body
AND / OR?
CombineLambdas(left, right, ExpressionType.AndAlso)
Get Paramters
>=
constantPost.Property
=>
post
Body
==
constantPost.Property
=>
post
Body
SubstituteParameterVisitor(left.Parameters[0], right.Parameters[0])
Replace Parameters
>=
constantPost.Property
Body
==
constantPost.Property
post
Body
post
Post.Property
SubstitutionVisitor.VisitParameter
Combined Expressions
=>
post
Lambda
parameter
Body
>=
&&
==
Expression.MakeBinary
Expression.Lambda<Func<T, bool>>
Readability?
Before
After
1 postRepository.GetAll()
2 .Where(post => post.IsPublished && post.PostedOn <= today &&
3 (post.PostedOn >= cutoffDate || post.Author == featuredAuthor &&
4 post.PostedOn >= featuredAuthorCutoffDate));
1 postRepository.GetAll()
2 .ArePublished()
3 .PostedOnOrBefore(today)
4 .Where(PostedOnOrAfter(cutoffDate)
5 .Or(FeaturedAuthorPostedrOnOrAfter(featuredAuthor,featuredAuthorCutoffDate)));
New Requirements!
Now we must support multiple featured authors
Dynamic
LINQ query?
No problem, now we’re equipped for it!
Dynamic Queries
Try this without expressions.
1 var rule = PredicateExtensions.PredicateExtensions.Begin<Post>();
2 foreach (var authorName in featuredAuthorNames)
3 {
4 rule = rule.Or(FeaturedAuthorPostedOnOrAfter(authorName, featuredAuthorCutoffDate));
5 }
6 return rule;
Dynamic Expression
Left
Body.NodeType == ExpressionType.Constant
Right
if (IsExpressionBodyConstant(left)) return (right);
>=
constantPost.Property
=>
post
Body
bool true
Are
Questions?
Ed Charbeneau
Developer Advocate for Progress<Telerik>
Author<T>
Podcast => “Eat Sleep Code the Official Telerik Podcast”
Twitter.Where(user = @EdCharbeneau)
Resources
• Article
– https://goo.gl/kAM05P
• GitHub
– https://github.com/EdCharbeneau/PredicateExtensions
• Telerik
– www.Telerik.com
• Twitter
– @EdCharbeneau

More Related Content

PDF
Python dictionary : past, present, future
PPT
Oop lecture7
PPTX
Basics of Python programming (part 2)
PDF
Scala - en bedre og mere effektiv Java?
PDF
Developing Applications with MySQL and Java for beginners
PDF
Python Workshop Part 2. LUG Maniapl
PPTX
Breaking down data silos with the open data protocol
PPTX
Chap1 array
Python dictionary : past, present, future
Oop lecture7
Basics of Python programming (part 2)
Scala - en bedre og mere effektiv Java?
Developing Applications with MySQL and Java for beginners
Python Workshop Part 2. LUG Maniapl
Breaking down data silos with the open data protocol
Chap1 array

What's hot (20)

PDF
Scala - en bedre Java?
PDF
Immutability, and how to do it in JavaScripts
PDF
Java script objects 1
 
PPTX
Datastructures in python
PPTX
Introduction to python programming 1
PPTX
Introduction to python programming 2
PDF
The Ring programming language version 1.7 book - Part 39 of 196
PDF
Aggregation Function in Druid
PPTX
Introduction to NOSQL And MongoDB
PDF
python and database
PDF
R for Pythonistas (PyData NYC 2017)
PPTX
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
PPTX
Python Training
PDF
PPTX
Python Datatypes by SujithKumar
PPTX
Ggplot2 v3
PPT
Python tutorialfeb152012
PPT
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
PPTX
Learn python - for beginners - part-2
PDF
Java chapter 6 - Arrays -syntax and use
Scala - en bedre Java?
Immutability, and how to do it in JavaScripts
Java script objects 1
 
Datastructures in python
Introduction to python programming 1
Introduction to python programming 2
The Ring programming language version 1.7 book - Part 39 of 196
Aggregation Function in Druid
Introduction to NOSQL And MongoDB
python and database
R for Pythonistas (PyData NYC 2017)
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
Python Training
Python Datatypes by SujithKumar
Ggplot2 v3
Python tutorialfeb152012
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
Learn python - for beginners - part-2
Java chapter 6 - Arrays -syntax and use
Ad

Similar to Giving Clarity to LINQ Queries by Extending Expressions R2 (20)

PPTX
Giving Clarity to LINQ Queries by Extending Expressions
PDF
New c sharp3_features_(linq)_part_iv
PPT
PDF
LINQ Inside
PPTX
Introducción a LINQ
PPTX
Link quries
PDF
DevNation'15 - Using Lambda Expressions to Query a Datastore
PPT
Understanding linq
PPT
Introduction to Linq
PPT
C# 3.0 Language Innovations
PPTX
Typed? Dynamic? Both! Cross-platform DSLs in C#
PPTX
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
ODP
New c sharp3_features_(linq)_part_iv
PPTX
Share pointtechies linqtosp-andsbs
PPT
Language Integrated Query - LINQ
PPTX
Think in linq
PDF
.NET Core, ASP.NET Core Course, Session 14
PPTX
Linq Introduction
PPT
Linq in C# 3.0: An Overview
Giving Clarity to LINQ Queries by Extending Expressions
New c sharp3_features_(linq)_part_iv
LINQ Inside
Introducción a LINQ
Link quries
DevNation'15 - Using Lambda Expressions to Query a Datastore
Understanding linq
Introduction to Linq
C# 3.0 Language Innovations
Typed? Dynamic? Both! Cross-platform DSLs in C#
Exploring C# DSLs: LINQ, Fluent Interfaces and Expression Trees
New c sharp3_features_(linq)_part_iv
Share pointtechies linqtosp-andsbs
Language Integrated Query - LINQ
Think in linq
.NET Core, ASP.NET Core Course, Session 14
Linq Introduction
Linq in C# 3.0: An Overview
Ad

More from Ed Charbeneau (19)

PPTX
Writing JavaScript for C# Blazor.pptx
PPTX
Blazor Stability Testing Tools for Bullet Proof Applications
PPTX
Secrets of a Blazor Component Artisan (v2)
PPTX
Modernizing Web Apps with .NET 6.pptx
PPTX
Modernizing Web Apps with .NET 6.pptx
PPTX
Blazor Full-Stack
PPTX
Secrets of a Blazor Component Artisan
PPTX
Writing java script for Csharp's Blazor
PPTX
Goodbye JavaScript Hello Blazor
PPTX
Razor into the Razor'verse
PPTX
PPTX
The future of .NET lightning talk
PPTX
Into the next dimension
PPTX
What is new in Q2 2015
PPTX
TelerikNEXT What's new in UI for ASP.NET AJAX
PPTX
Journey to JavaScript (from C#)
PPTX
Refactoring css
PPTX
Don't be a stereotype: Rapid Prototype
PPTX
A crash course in responsive design
Writing JavaScript for C# Blazor.pptx
Blazor Stability Testing Tools for Bullet Proof Applications
Secrets of a Blazor Component Artisan (v2)
Modernizing Web Apps with .NET 6.pptx
Modernizing Web Apps with .NET 6.pptx
Blazor Full-Stack
Secrets of a Blazor Component Artisan
Writing java script for Csharp's Blazor
Goodbye JavaScript Hello Blazor
Razor into the Razor'verse
The future of .NET lightning talk
Into the next dimension
What is new in Q2 2015
TelerikNEXT What's new in UI for ASP.NET AJAX
Journey to JavaScript (from C#)
Refactoring css
Don't be a stereotype: Rapid Prototype
A crash course in responsive design

Recently uploaded (20)

PDF
Architecture types and enterprise applications.pdf
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
PPT
What is a Computer? Input Devices /output devices
PDF
1 - Historical Antecedents, Social Consideration.pdf
PDF
Hybrid model detection and classification of lung cancer
PPTX
Tartificialntelligence_presentation.pptx
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Unlock new opportunities with location data.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Architecture types and enterprise applications.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
observCloud-Native Containerability and monitoring.pptx
Assigned Numbers - 2025 - Bluetooth® Document
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
A contest of sentiment analysis: k-nearest neighbor versus neural network
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Zenith AI: Advanced Artificial Intelligence
Final SEM Unit 1 for mit wpu at pune .pptx
What is a Computer? Input Devices /output devices
1 - Historical Antecedents, Social Consideration.pdf
Hybrid model detection and classification of lung cancer
Tartificialntelligence_presentation.pptx
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
NewMind AI Weekly Chronicles – August ’25 Week III
A comparative study of natural language inference in Swahili using monolingua...
Benefits of Physical activity for teenagers.pptx
Unlock new opportunities with location data.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx

Giving Clarity to LINQ Queries by Extending Expressions R2

Editor's Notes

  • #11: https://dotnetfiddle.net/r5UyK0
  • #12: https://dotnetfiddle.net/r5UyK0
  • #13: https://dotnetfiddle.net/r5UyK0
  • #14: https://dotnetfiddle.net/r5UyK0
  • #15: https://dotnetfiddle.net/kE2uaf
  • #16: https://dotnetfiddle.net/kE2uaf
  • #17: https://dotnetfiddle.net/9a53zl
  • #22: step-1
  • #23: step-1
  • #25: Step-2
  • #26: Step-2
  • #27: Step-2
  • #28: Step-2
  • #29: Step-3
  • #31: step-4
  • #32: Step-5 (intended to fail / not compile)
  • #33: Step-5 (intended to fail / not compile)
  • #34: Step-5 (intended to fail / not compile)
  • #35: https://dotnetfiddle.net/9a53zl
  • #38: Replaces all {post} instances with the same post parameter on both left and right expressions.
  • #39: Step-6
  • #40: Step-7 (refactor)
  • #42: Step-8
  • #43: This facilitates the Begin<T> method of the predicate extension. This part of the code strips off the => true constant expression from the left side.