![Page 1: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/1.jpg)
Introduction of Lambda Expression and Predicate BuilderBryan lin
2014/03/07
![Page 2: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/2.jpg)
Agenda
• Lambda Expression
• PredicateBuilder
• Implementation of PredicateBuilder
![Page 3: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/3.jpg)
Lambda expression
• What is a Lambda Expression?
• A lambda expression is an anonymous function and it is mostly used to create delegates in LINQ. Simply put, it's a method without a declaration, i.e., access modifier, return value declaration, and name.
![Page 4: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/4.jpg)
Lambda expression
• Why do we need lambda expressions? (Why would we need to write a method without a name?)
• Convenience. It's a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.
![Page 5: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/5.jpg)
Lambda expression
• Benefits:
• Reduced typing. No need to specify the name of the function, its return type, and its access modifier.
• When reading the code you don't need to look elsewhere for the method's definition.
![Page 6: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/6.jpg)
Lambda expression
• Lambda expressions should be short. A complex definition makes the calling code difficult to read.
![Page 7: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/7.jpg)
Lambda expression
• How do we define a lambda expression?
• Lambda basic definition: Parameters => Executed code.
![Page 8: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/8.jpg)
PredicateBuilder
• Background
• Dynamic Predicate Construction
• You allow the user to pick, from a check box list, any of the fields that will be included in the query, and to specify the criteria
![Page 9: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/9.jpg)
PredicateBuilder
![Page 10: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/10.jpg)
PredicateBuilder
• Background
• Dynamic Predicate Construction
• You can do a bunch of switch-ing if logic to figure out what kind of query to render by adding a piece of it at a time
• Or you can use lambdas and the PredicateBuilder to inject multiple predicates into an expression tree
![Page 11: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/11.jpg)
PredicateBuilder
• Using the Code
• To combine the two criteria below:
![Page 12: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/12.jpg)
PredicateBuilder
• Using the Code
• Do with the PredcateBuilder:
![Page 13: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/13.jpg)
PredicateBuilder
• Using the Code
• And then:
![Page 14: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/14.jpg)
Implementation of PredicateBuilder
• Implmentation of and logic:
![Page 15: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/15.jpg)
Implementation of PredicateBuilder
• Implementation of or logic:
![Page 16: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/16.jpg)
References
• http://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-3-minutes
• http://www.codeproject.com/Articles/28580/LINQ-and-Dynamic-Predicate-Construction-at-Runtime
• http://www.albahari.com/nutshell/predicatebuilder.aspx
![Page 17: Introduction of lambda expression and predicate builder](https://reader035.vdocuments.us/reader035/viewer/2022062405/554ebf4eb4c905064d8b47fe/html5/thumbnails/17.jpg)
FAQ