19
Road to Dynamic LINQ – Part 2 Vedran Maršić AKA Fosna Warning: Expression Trees Ahead

Road to Dynamic LINQ - Part 2

  • Upload
    axilis

  • View
    351

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Road to Dynamic LINQ - Part 2

Road to Dynamic LINQ – Part 2

Vedran Maršić AKA Fosna

Warning: Expression Trees Ahead

Page 2: Road to Dynamic LINQ - Part 2

About me Vedran Maršić AKA Fosna

dev at http://www.axilis.com/author/vmarsic/

organizing programming workshops for kids Logo, Scratch, HTML, CSS, JavaScript, Python, C++

Page 3: Road to Dynamic LINQ - Part 2

We’ll cover Part 1

What is expression tree in C# How is it different from similar looking lambda delegates How to build expression trees How to parse expression trees Where are you already using them

Part 2 Check out few expression tree exploits Point to brilliant, not easy to understand, ideas or projects from some cool

developer maniacs

Page 4: Road to Dynamic LINQ - Part 2

We won’t cover Implementing LINQ providers Implementing deep copy Implementing faster static dictionary Implementing IoC container

Page 5: Road to Dynamic LINQ - Part 2

Need for Dynamic Queries

Page 6: Road to Dynamic LINQ - Part 2
Page 7: Road to Dynamic LINQ - Part 2
Page 8: Road to Dynamic LINQ - Part 2
Page 9: Road to Dynamic LINQ - Part 2

Applied Expression TreesC# 6.0 Taste Before C# 6.0

Page 10: Road to Dynamic LINQ - Part 2

Sort by Custom Property... in LINQ to Entity Framework

Try to support sort by each column in grid on web page Chances are grid will send to the server

a string for property name and a sort order

Page 11: Road to Dynamic LINQ - Part 2

Dynamic Searching... in LINQ to Entity Framework Try to support search by each column in grid on web page

Chances are grid will send to the server a property name and a value to compare to

Let’s not bother with more complex requirements for brevity of the presentation

Page 12: Road to Dynamic LINQ - Part 2

Get Property or Method NamesSomething like nameof operators in C# 6.0

Similar thing was possible with older compiler

Hint:

Page 13: Road to Dynamic LINQ - Part 2

Get Over NULL Reference Exceptions Null-conditional operators in C# 6.0

Empty string if person is null or person.Child is null

Simmilar thing was possible with older compilerstring childName = person.Safely(x => x.Child.Name);

Page 14: Road to Dynamic LINQ - Part 2

Source Code Available at

https://github.com/axilis/RoadToDynamicLinq

Page 15: Road to Dynamic LINQ - Part 2

Some Other Ideas LINQ update, insert, delete statement with where clause

without dbContext.SaveChanges() magic Entity Framework Extended Library by Paul Welter of LoreSoft https://github.com/loresoft/EntityFramework.Extended

TypeSafe property changed http://

stackoverflow.com/questions/3551947/in-c-why-expression-trees-and-when-do-you-need-to-use-them/3551991#3551991

Lambda equality comparer with delegates

Page 16: Road to Dynamic LINQ - Part 2

Stay Away from Other Ideas Deep copy Customizable convention based copy, like AutoMapper Fast IoC Container

BarbarianIOC : A simple IOC Container by Sacha Barber, 25 Feb 2013 http://www.codeproject.com/Articles/552514/BarbarianIOC-A-simple-IOC-Container

Faster static dictionary StaticStringDictionary - Fast Switching With LINQ Revisited, AUG 28TH, 2008, by Gustavo Guerra http://functionalflow.co.uk/blog/2008/08/28/staticstringdictionary-fast-switching-with-linq-revisited/

Your own LINQ provider Using Expression Trees in Your APIs by Patrick Steele04/01/2011 https://visualstudiomagazine.com/Articles/2011/04/01/pccsp_Expression-Trees.aspx

Page 17: Road to Dynamic LINQ - Part 2

Thanks. Bye!No questions, right?

Page 18: Road to Dynamic LINQ - Part 2

Resources Start with

http://stackoverflow.com/a/403233/953338 Expression Tree Basics, Charlie Calvert 31 Jan 2008 3:49 AM

http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

Published by Olexander Ivanitskyi on Tuesday, June 11, 2013 LINQ: Func<T> vs. Expression<Func<T>> http://ivanitskyi.blogspot.hr/2013/06/linq-func-vs-expression.html

 asked Apr 27 '09 at 13:50, Richard Nagle Why would you use Expression<Func<T>> rather than Func<T>? http://

stackoverflow.com/questions/793571/why-would-you-use-expressionfunct-rather-than-funct