SlideShare a Scribd company logo
James Michael Hare
                                   2012 Visual C# MVP
                                  Application Architect
                                              Scottrade
                                        August 3rd, 2012


http://www.BlackRabbitCoder.net
Twitter: @BlkRabbitCoder
Me:
  Blog: http://www.BlackRabbitCoder.net
  Twitter: @BlkRabbitCoder


Information on Scottrade Careers:
  http://jobs.scottrade.com
  Twitter: @scottradejobs
Introduction
LINQ and lambdas are becoming much more
 prevalent in our codebases and it’s important for all of
 our developers to have at least a basic
 understanding.
This presentation is designed to be a brief
 introduction to both lambda expressions and the
 more common LINQ extension methods.
In addition, we will give a brief overview of the
 optional query syntax.
A brief history…
Before we look at lambdas, let’s take a look at how
 delegates work in C#.
A delegate is similar to a pointer to a function in C++.
Delegate describe the signature of a method.
Define delegates using the keyword delegate.
The following delegate describes methods that have
 no return value and take a string parameter:
Defining matching methods
Method can be static or instance.
These methods match our LoggingMethod
 delegate:
Assigning instance methods
Delegate instances can be assigned method
 references.
If instance method assigned, provide the instance to
 invoke delegate from:




The instance may be omitted if assigned from an
 instance member of the same instance.
Assigning static methods
Since static methods require no instance to be
 invoked upon, just the class name.




The class name may be omitted if the current class is
 the class that contains the method to be assigned.
Invoking a delegate
The beauty of delegates is that they allow methods to
 be assigned to variables.
This means you can easily change the behavior of a
 piece of code without needing to subclass.
You invoke by calling the delegate like the method:
Anonymous methods
The only problem with early .NET was that all
 delegate targets had to be methods.
This means you’d have to create full methods for even
 the simplest task.
The anonymous method syntax introduced in .NET
 2.0 allowed writing methods on the fly at the point
 they would be assigned to a delegate:
Lambda expressions
.NET 3.0 added lambda expression support for
 writing anonymous methods.
Lambda expression syntax can be seen as a more
 concise form of the anonymous method syntax:




The full lambda expression syntax is:
      (parameters) => { statement(s) }
The lambda operator “=>” is pronounced “goes to”.
More on lambda parameters
Specifying type is optional when can be inferred
    (string s) => { return s.Length; }
    (s) => { return s.Length; }
If no parameters, the parenthesis are empty:
    () => { statement(s) }
If one parameter, the parenthesis are optional:
     (s) => { return s.Length; }
     s => { return s.Length; }
If several parameters, use parenthesis and commas:
     (x,y) => { return x > y; }
Lambda parameter naming
The standard convention is a short identifier whose
 meaning can be inferred from context:




Names are local to the lambda and can be reused if
 desired to represent an item flowing down a chain.
More on lambda body
If body more than one statement, must separate with
 semicolons and enclose in braces:
    s => {
             Console.WriteLine(s);
             Console.Out.Flush();
         }
If one statement only, can omit semicolon and braces:
     s => { Console.Error.WriteLine(s); }
     s => Console.Error.WriteLine(s)
If only an expression to evaluate, can omit return:
     s => return s.Length
     s => s.Length
The generic delegates
Delegates can be useful for specifying pluggable
 behavior at runtime instead of compile time.
This eliminates much of the need of inheritance in
 places it was used to specify different behaviors.
.NET 2.0 added some common generic delegates:
  Predicate<T> - bool fx(T) – specifies a condition to be
   run against an item which returns true or false.
  Action<T> - void fx(T) – specifies an action to be
   performed on an item, no result.
  Action has other versions as well for varying number of
   parameters from 0 to 16.
More generic delegates
.NET 3.5 added a new generic delegate for more
 general delegate specification:
  Func<TResult> - TResult fx() – specifies a delegate
   that takes no parameters and returns a TResult.
  Func<T, TResult> - TResult fx(T) – specifies a delegate
   that takes on parameter and returns a TResult.
  Func<T1, T2, TResult> - TResult fx(T1, T2) – specifies a
   delegate that takes 2 parameters and returns TResult.
  Func<T1…, TResult> can support up to 16 parameters.
Func<T, bool> is equivalent to Predicate<T>.
Pluggable behavior
Delegates allow you to create classes and algorithms
 with pluggable behavior without inheritance:
Pluggable behavior
Behaviors can be changed on the fly:
LINQ
LINQ = Language INtegrated Query.
LINQ is a new query language and class libraries.
LINQ class libraries can be used with or without the
 query syntax as desired.
Most LINQ extension methods operate on sequences
 of data that implement IEnumerable<T>.
Most LINQ extension methods specify behavior with
 generic delegates (Action, Func, etc).
Lambda expressions are a perfect way to supply
 concise definitions to LINQ.
LINQ: Common behaviors
Most LINQ extension methods cannot be called on a
 null sequence (throws ArgumentNullException).
You can specify behaviors using any valid means
 (lambda expressions, anonymous methods, ordinary
 methods, or method groups).
You can use the LINQ methods on any sequences
 that implement IEnumerable<T> including List<T>,
 T[], HashSet<T>, iterators, etc.
Many of the LINQ methods use deferred execution
 (may not compute query until the data is needed).
LINQ: Chaining
LINQ operations can be chained together.
Each method call is independent and applies to the
 previous result in the chain:


   Where() narrows sequence to only those items whose Value
    property has an IsExpired property == true.
   Select() transforms the expired items from the Where() to return a
    sequence containing only the Key properties.
   ToList() takes the sequence of expired Keys from the Select() and
    returns then in a List<string>.
LINQ: Selecting and filtering
Where() – Narrows sequence based on condition
  orders.Where(o => o.Type == OrderType.Buy)
       Sequence only containing orders with order type of buy.
Select() – Transforms items in sequence or returns
 part(s) of items.
  orders.Select(o => o.Type)
       Sequence only containing the types of the orders.
These two are sometimes confused, remember not to
 use Select to try to filter or you get a sequence of bool.
  orders.Select(o => o.Type == OrderType.Buy)
LINQ: Consistency checks
All() – True if all items satisfy a predicate:
  requests.All(r => r.IsValid)
       True if IsValid returns true for all items.
  employees.All(e => e.Ssn != null)
       True if Ssn is not null for all items.
Any() – True if at least one satisfies predicate:
  requests.Any(r => !r.IsValid)
       True if any item returns IsValid of false.
  results.Addresses.Any()
       True if Addresses contains at least one item.
LINQ: Membership and count
Contains() – True if sequence contains item.
  orderTypes.Contains(“Buy”)
       True if contains string “buy” based on default string comparer.
  orderTypes.Contains(“buy”, comparer)
       True if contains “buy” based on given string comparer.
Count() – Number of items (or matches) in
 sequence.
  requests.Count(r => !r.IsValid)
       Count of items where IsValid returns false
  requests.Count()
       Count of all items in sequence
LINQ: Combine and reduce
Distinct() – Sequence without duplicates.
  orderIds.Distinct()
       A list of all unique order ids based on default comparer.
Concat() – Concatenates sequences.
  defaultValues.Concat(otherValues)
       Sequence containing defaultValues followed by otherValues.
Union() – Concatenates without duplicates.
  defaultValues.Union(otherValues)
       Sequence with items from defaultValues followed by items
        from otherValues without any duplicates.
LINQ: First item or match
First() – Returns first item or match, throws if none.
  orders.First()
     Returns first order, throws if none.
  orders.First(o => o.Type == OrderType.Buy)
     Returns first buy order, throws if no buy orders.

FirstOrDefault() – Returns first item, default if
 none.
  orders.FirstOrDefault()
     Returns first order, default if no orders.
     Default based on type (null for reference, 0 for numeric, etc)

  orders.FirstOrDefault(o => o.Id > 100)
     Returns first order with id > 100, or default if no match.
LINQ: Other items and matches
Last() / LastOrDefault()
  Similar to First except returns last match or item in list.
  Default version returns default of type if no match.
ElementAt() / ElementAtOrDefault()
  Returns element at the given position (zero-indexed).
  Default version returns default of type if position >
    count.
Single() / SingleOrDefault()
  Very similar to First except ensures that only one item
    exists that matches.
LINQ: Set operations
Except() – Set difference, subtracts sequences
  snackFoods.Except(healthyFoods)
     Returns first sequence minus elements in the second.
     Sequence of snack foods that are not healthy foods.

Intersect() – Set intersection, common sequence
  snackFoods.Intersect(healthyFoods)
     Returns first sequence minus elements in the second.
     Sequence of foods that are snack foods and healthy foods.

Union() – Combines sequences without duplicates.
  snackFoods.Union(healthyFoods)
       Returns both sequences combined with duplicates removed.
LINQ: Aggregation operations
Sum() – Adds all in from a sequence
  orders.Sum(o => o.Quantity)
Average() – Agerage of all items in a sequence
  orders.Average(o => o.Quantity)
Min()– Finds minimum item in sequence
  orders.Min(o => o.Quantity)
Max() – Finds maximum item in sequence
  orders.Max(o => o.Quantity)
Aggregate() – Performs custom aggregation
  orders.Aggregate(0.0, (total, o) =>
      total += o.Quantity * o.Price)
LINQ: Grouping
GroupBy() – Groups a sequence by criteria
  orders.GroupBy(o => o.Type)
     Organizes the sequence into groups of sequences.
     In this case, organizes orders into sequences by order type.

     Each element in resulting sequence is grouping with Key being
      the group criteria and the grouping being a sequence:
LINQ: Ordering
OrderBy() – Orders in ascending order
  orders.OrderBy(o => o.Id)
       Orders in ascending order by order id.
OrderByDescending() – Orders in descending order
  Orders.OrderByDescending(o => o.Id)
       Orders in descending order by order id.
ThenBy() – Adds additional ascending criteria
  Orders.OrderBy(o => o.Id).ThenBy(o => o.Type)
       Orders in ascending order by id and when ids are same ordered
        by order type as a secondary ordering.
ThenByDescending() – As above, but descending.
LINQ: Exporting to Collection
ToArray() – Returns sequence as an array.
  orders.OrderBy(o => o.Id).ToArray()
       Retrieves orders ordered by id into an array.
ToList() – Returns sequence as a List
  Orders.OrderBy(o => o.Id).ToList()
       Retrieves orders ordered by id into a List<Order>.
ToDictionary() – Returns sequence as Dictionary
  Orders.ToDictionary(o => o.Id)
       Retrieves orders in a dictionary where the Id is the key.
ToLookup() – Returns sequence of IGrouping
  Orders.ToLookup(o => o.Type)
       Retrieves orders into groups of orders keyed by order type.
Query Syntax
The query syntax is an alternative way to write LINQ
 expressions.
Microsoft tends to recommend the query syntax as it
 tends to be easier to read.
Ultimately, it is reduced to the same method calls
 we’ve already seen, so can use either.
There are some methods which are not directly
 callable using the query syntax.
Query Syntax
All queries start with the from clause in the form:
  from o in orders
     Declares variable representing current and source sequence.
     Similar to foreach(var o in orders) except deferred.

All queries must end with a select or group clause:
  from o in orders select o.Id
       Projects output same as the Select() extension method.
  from o in orders group o by o.Id
       Groups output same as the GroupBy() extension method.
Query Syntax
In between the from and the select/group clauses,
 you can filter, order, and join as desired:
  where – filters to only matching objects, like Where().
  into – allows storage of results from a groupby, join, or
   select for use in later clauses.
  order – sorts the results, can use ascending or
   descending modifiers, like the OrderBy() method
   family.
  join – joins two sequences together, like Join().
  let – allows storage of result from sub-expression for use
   in later clauses.
Same Query, Different Syntax
These two queries are the same, just different syntax:
Pros and cons
Pros:
  Lambda expressions are much more concise than full
   methods for simple tasks.
  LINQ expressions simplifies implementation by
   using .NET libraries that are optimized and fully tested.
  Once you learn the lambdas and LINQ, ultimately the
   code is much more readable and easier to maintain.
Cons:
  The lambda syntax and LINQ methods take some
   getting used to at first (short ramp-up time).
Summary
Delegates help create more modular code without the
 need for inheritance.
Lambdas are just an easy way to assign an anonymous
 method to a delegate.
LINQ introduces a great class library for performing
 common operations on sequences of data.
Using lambdas and LINQ can help reduce code size
 and improve code readability and maintainability.
Care should be taken to make lambda expressions
 concise and meaningful while still readable.
Questions?
Platinum
Sponsors



Gold
Sponsors




Silver
Sponsors
Ad

More Related Content

What's hot (20)

Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Mohammed Rafi
 
Set methods in python
Set methods in pythonSet methods in python
Set methods in python
deepalishinkar1
 
Implementation Model
Implementation ModelImplementation Model
Implementation Model
Fáber D. Giraldo
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
Support JEE Servlet Jsp MVC M.Youssfi
Support JEE Servlet Jsp MVC M.YoussfiSupport JEE Servlet Jsp MVC M.Youssfi
Support JEE Servlet Jsp MVC M.Youssfi
ENSET, Université Hassan II Casablanca
 
Java Programming - 05 access control in java
Java Programming - 05 access control in javaJava Programming - 05 access control in java
Java Programming - 05 access control in java
Danairat Thanabodithammachari
 
Nodejs buffers
Nodejs buffersNodejs buffers
Nodejs buffers
monikadeshmane
 
Support Web Services SOAP et RESTful Mr YOUSSFI
Support Web Services SOAP et RESTful Mr YOUSSFISupport Web Services SOAP et RESTful Mr YOUSSFI
Support Web Services SOAP et RESTful Mr YOUSSFI
ENSET, Université Hassan II Casablanca
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
Mario Sangiorgio
 
Rest assured
Rest assuredRest assured
Rest assured
Varun Deshpande
 
Lecture04- Use Case Diagrams
Lecture04- Use Case DiagramsLecture04- Use Case Diagrams
Lecture04- Use Case Diagrams
artgreen
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
John Ferguson Smart Limited
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions
oysteing
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
Priyanka Pradhan
 
Les dessous du framework spring
Les dessous du framework springLes dessous du framework spring
Les dessous du framework spring
Antoine Rey
 
Initiation à l'algorithmique
Initiation à l'algorithmiqueInitiation à l'algorithmique
Initiation à l'algorithmique
Abdoulaye Dieng
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
Shellmates
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
Sangharsh agarwal
 
Spring cheat sheet
Spring cheat sheetSpring cheat sheet
Spring cheat sheet
Mark Papis
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Mohammed Rafi
 
Angularjs - Unit testing introduction
Angularjs - Unit testing introductionAngularjs - Unit testing introduction
Angularjs - Unit testing introduction
Nir Kaufman
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
Ashita Agrawal
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
Mario Sangiorgio
 
Lecture04- Use Case Diagrams
Lecture04- Use Case DiagramsLecture04- Use Case Diagrams
Lecture04- Use Case Diagrams
artgreen
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions
oysteing
 
programming with python ppt
programming with python pptprogramming with python ppt
programming with python ppt
Priyanka Pradhan
 
Les dessous du framework spring
Les dessous du framework springLes dessous du framework spring
Les dessous du framework spring
Antoine Rey
 
Initiation à l'algorithmique
Initiation à l'algorithmiqueInitiation à l'algorithmique
Initiation à l'algorithmique
Abdoulaye Dieng
 
Introduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El HassaniIntroduction à Python - Achraf Kacimi El Hassani
Introduction à Python - Achraf Kacimi El Hassani
Shellmates
 
Spring cheat sheet
Spring cheat sheetSpring cheat sheet
Spring cheat sheet
Mark Papis
 

Viewers also liked (8)

C#/.NET Little Pitfalls
C#/.NET Little PitfallsC#/.NET Little Pitfalls
C#/.NET Little Pitfalls
BlackRabbitCoder
 
C#/.NET Little Wonders
C#/.NET Little WondersC#/.NET Little Wonders
C#/.NET Little Wonders
BlackRabbitCoder
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
BlackRabbitCoder
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
BlackRabbitCoder
 
What is new in C# 6?
What is new in C# 6?What is new in C# 6?
What is new in C# 6?
Robert MacLean
 
What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6
Amir Barylko
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
Eric Phan
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6
Fiyaz Hasan
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
BlackRabbitCoder
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
BlackRabbitCoder
 
What's new in c# 6
What's new in c# 6What's new in c# 6
What's new in c# 6
Amir Barylko
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
Eric Phan
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6
Fiyaz Hasan
 
Ad

Similar to Of Lambdas and LINQ (20)

Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
Michal Bigos
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
MLG College of Learning, Inc
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
Java 8 Intro - Core Features
Java 8 Intro - Core FeaturesJava 8 Intro - Core Features
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
Dian Aditya
 
JDK8 Lambda expressions demo
JDK8 Lambda expressions demoJDK8 Lambda expressions demo
JDK8 Lambda expressions demo
MouryaKumar Reddy Rajala
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Scala oo (1)
Scala oo (1)Scala oo (1)
Scala oo (1)
Sandip Kumar
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak
 
Linq and lambda
Linq and lambdaLinq and lambda
Linq and lambda
John Walsh
 
Xebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_finalXebicon2013 scala vsjava_final
Xebicon2013 scala vsjava_final
Urs Peter
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
Michal Bigos
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
Van Huong
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya UdagedaraLambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Lambda Expressions in C# From Beginner To Expert - Jaliya Udagedara
Jaliya Udagedara
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
tjunicornfx
 
What's new in java 8
What's new in java 8What's new in java 8
What's new in java 8
Dian Aditya
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak
 
Ad

Recently uploaded (20)

Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 

Of Lambdas and LINQ

  • 1. James Michael Hare 2012 Visual C# MVP Application Architect Scottrade August 3rd, 2012 http://www.BlackRabbitCoder.net Twitter: @BlkRabbitCoder
  • 2. Me: Blog: http://www.BlackRabbitCoder.net Twitter: @BlkRabbitCoder Information on Scottrade Careers: http://jobs.scottrade.com Twitter: @scottradejobs
  • 3. Introduction LINQ and lambdas are becoming much more prevalent in our codebases and it’s important for all of our developers to have at least a basic understanding. This presentation is designed to be a brief introduction to both lambda expressions and the more common LINQ extension methods. In addition, we will give a brief overview of the optional query syntax.
  • 4. A brief history… Before we look at lambdas, let’s take a look at how delegates work in C#. A delegate is similar to a pointer to a function in C++. Delegate describe the signature of a method. Define delegates using the keyword delegate. The following delegate describes methods that have no return value and take a string parameter:
  • 5. Defining matching methods Method can be static or instance. These methods match our LoggingMethod delegate:
  • 6. Assigning instance methods Delegate instances can be assigned method references. If instance method assigned, provide the instance to invoke delegate from: The instance may be omitted if assigned from an instance member of the same instance.
  • 7. Assigning static methods Since static methods require no instance to be invoked upon, just the class name. The class name may be omitted if the current class is the class that contains the method to be assigned.
  • 8. Invoking a delegate The beauty of delegates is that they allow methods to be assigned to variables. This means you can easily change the behavior of a piece of code without needing to subclass. You invoke by calling the delegate like the method:
  • 9. Anonymous methods The only problem with early .NET was that all delegate targets had to be methods. This means you’d have to create full methods for even the simplest task. The anonymous method syntax introduced in .NET 2.0 allowed writing methods on the fly at the point they would be assigned to a delegate:
  • 10. Lambda expressions .NET 3.0 added lambda expression support for writing anonymous methods. Lambda expression syntax can be seen as a more concise form of the anonymous method syntax: The full lambda expression syntax is: (parameters) => { statement(s) } The lambda operator “=>” is pronounced “goes to”.
  • 11. More on lambda parameters Specifying type is optional when can be inferred (string s) => { return s.Length; } (s) => { return s.Length; } If no parameters, the parenthesis are empty: () => { statement(s) } If one parameter, the parenthesis are optional: (s) => { return s.Length; } s => { return s.Length; } If several parameters, use parenthesis and commas: (x,y) => { return x > y; }
  • 12. Lambda parameter naming The standard convention is a short identifier whose meaning can be inferred from context: Names are local to the lambda and can be reused if desired to represent an item flowing down a chain.
  • 13. More on lambda body If body more than one statement, must separate with semicolons and enclose in braces: s => { Console.WriteLine(s); Console.Out.Flush(); } If one statement only, can omit semicolon and braces: s => { Console.Error.WriteLine(s); } s => Console.Error.WriteLine(s) If only an expression to evaluate, can omit return: s => return s.Length s => s.Length
  • 14. The generic delegates Delegates can be useful for specifying pluggable behavior at runtime instead of compile time. This eliminates much of the need of inheritance in places it was used to specify different behaviors. .NET 2.0 added some common generic delegates: Predicate<T> - bool fx(T) – specifies a condition to be run against an item which returns true or false. Action<T> - void fx(T) – specifies an action to be performed on an item, no result. Action has other versions as well for varying number of parameters from 0 to 16.
  • 15. More generic delegates .NET 3.5 added a new generic delegate for more general delegate specification: Func<TResult> - TResult fx() – specifies a delegate that takes no parameters and returns a TResult. Func<T, TResult> - TResult fx(T) – specifies a delegate that takes on parameter and returns a TResult. Func<T1, T2, TResult> - TResult fx(T1, T2) – specifies a delegate that takes 2 parameters and returns TResult. Func<T1…, TResult> can support up to 16 parameters. Func<T, bool> is equivalent to Predicate<T>.
  • 16. Pluggable behavior Delegates allow you to create classes and algorithms with pluggable behavior without inheritance:
  • 17. Pluggable behavior Behaviors can be changed on the fly:
  • 18. LINQ LINQ = Language INtegrated Query. LINQ is a new query language and class libraries. LINQ class libraries can be used with or without the query syntax as desired. Most LINQ extension methods operate on sequences of data that implement IEnumerable<T>. Most LINQ extension methods specify behavior with generic delegates (Action, Func, etc). Lambda expressions are a perfect way to supply concise definitions to LINQ.
  • 19. LINQ: Common behaviors Most LINQ extension methods cannot be called on a null sequence (throws ArgumentNullException). You can specify behaviors using any valid means (lambda expressions, anonymous methods, ordinary methods, or method groups). You can use the LINQ methods on any sequences that implement IEnumerable<T> including List<T>, T[], HashSet<T>, iterators, etc. Many of the LINQ methods use deferred execution (may not compute query until the data is needed).
  • 20. LINQ: Chaining LINQ operations can be chained together. Each method call is independent and applies to the previous result in the chain:  Where() narrows sequence to only those items whose Value property has an IsExpired property == true.  Select() transforms the expired items from the Where() to return a sequence containing only the Key properties.  ToList() takes the sequence of expired Keys from the Select() and returns then in a List<string>.
  • 21. LINQ: Selecting and filtering Where() – Narrows sequence based on condition orders.Where(o => o.Type == OrderType.Buy)  Sequence only containing orders with order type of buy. Select() – Transforms items in sequence or returns part(s) of items. orders.Select(o => o.Type)  Sequence only containing the types of the orders. These two are sometimes confused, remember not to use Select to try to filter or you get a sequence of bool. orders.Select(o => o.Type == OrderType.Buy)
  • 22. LINQ: Consistency checks All() – True if all items satisfy a predicate: requests.All(r => r.IsValid)  True if IsValid returns true for all items. employees.All(e => e.Ssn != null)  True if Ssn is not null for all items. Any() – True if at least one satisfies predicate: requests.Any(r => !r.IsValid)  True if any item returns IsValid of false. results.Addresses.Any()  True if Addresses contains at least one item.
  • 23. LINQ: Membership and count Contains() – True if sequence contains item. orderTypes.Contains(“Buy”)  True if contains string “buy” based on default string comparer. orderTypes.Contains(“buy”, comparer)  True if contains “buy” based on given string comparer. Count() – Number of items (or matches) in sequence. requests.Count(r => !r.IsValid)  Count of items where IsValid returns false requests.Count()  Count of all items in sequence
  • 24. LINQ: Combine and reduce Distinct() – Sequence without duplicates. orderIds.Distinct()  A list of all unique order ids based on default comparer. Concat() – Concatenates sequences. defaultValues.Concat(otherValues)  Sequence containing defaultValues followed by otherValues. Union() – Concatenates without duplicates. defaultValues.Union(otherValues)  Sequence with items from defaultValues followed by items from otherValues without any duplicates.
  • 25. LINQ: First item or match First() – Returns first item or match, throws if none. orders.First()  Returns first order, throws if none. orders.First(o => o.Type == OrderType.Buy)  Returns first buy order, throws if no buy orders. FirstOrDefault() – Returns first item, default if none. orders.FirstOrDefault()  Returns first order, default if no orders.  Default based on type (null for reference, 0 for numeric, etc) orders.FirstOrDefault(o => o.Id > 100)  Returns first order with id > 100, or default if no match.
  • 26. LINQ: Other items and matches Last() / LastOrDefault() Similar to First except returns last match or item in list. Default version returns default of type if no match. ElementAt() / ElementAtOrDefault() Returns element at the given position (zero-indexed). Default version returns default of type if position > count. Single() / SingleOrDefault() Very similar to First except ensures that only one item exists that matches.
  • 27. LINQ: Set operations Except() – Set difference, subtracts sequences snackFoods.Except(healthyFoods)  Returns first sequence minus elements in the second.  Sequence of snack foods that are not healthy foods. Intersect() – Set intersection, common sequence snackFoods.Intersect(healthyFoods)  Returns first sequence minus elements in the second.  Sequence of foods that are snack foods and healthy foods. Union() – Combines sequences without duplicates. snackFoods.Union(healthyFoods)  Returns both sequences combined with duplicates removed.
  • 28. LINQ: Aggregation operations Sum() – Adds all in from a sequence orders.Sum(o => o.Quantity) Average() – Agerage of all items in a sequence orders.Average(o => o.Quantity) Min()– Finds minimum item in sequence orders.Min(o => o.Quantity) Max() – Finds maximum item in sequence orders.Max(o => o.Quantity) Aggregate() – Performs custom aggregation orders.Aggregate(0.0, (total, o) => total += o.Quantity * o.Price)
  • 29. LINQ: Grouping GroupBy() – Groups a sequence by criteria orders.GroupBy(o => o.Type)  Organizes the sequence into groups of sequences.  In this case, organizes orders into sequences by order type.  Each element in resulting sequence is grouping with Key being the group criteria and the grouping being a sequence:
  • 30. LINQ: Ordering OrderBy() – Orders in ascending order orders.OrderBy(o => o.Id)  Orders in ascending order by order id. OrderByDescending() – Orders in descending order Orders.OrderByDescending(o => o.Id)  Orders in descending order by order id. ThenBy() – Adds additional ascending criteria Orders.OrderBy(o => o.Id).ThenBy(o => o.Type)  Orders in ascending order by id and when ids are same ordered by order type as a secondary ordering. ThenByDescending() – As above, but descending.
  • 31. LINQ: Exporting to Collection ToArray() – Returns sequence as an array. orders.OrderBy(o => o.Id).ToArray()  Retrieves orders ordered by id into an array. ToList() – Returns sequence as a List Orders.OrderBy(o => o.Id).ToList()  Retrieves orders ordered by id into a List<Order>. ToDictionary() – Returns sequence as Dictionary Orders.ToDictionary(o => o.Id)  Retrieves orders in a dictionary where the Id is the key. ToLookup() – Returns sequence of IGrouping Orders.ToLookup(o => o.Type)  Retrieves orders into groups of orders keyed by order type.
  • 32. Query Syntax The query syntax is an alternative way to write LINQ expressions. Microsoft tends to recommend the query syntax as it tends to be easier to read. Ultimately, it is reduced to the same method calls we’ve already seen, so can use either. There are some methods which are not directly callable using the query syntax.
  • 33. Query Syntax All queries start with the from clause in the form: from o in orders  Declares variable representing current and source sequence.  Similar to foreach(var o in orders) except deferred. All queries must end with a select or group clause: from o in orders select o.Id  Projects output same as the Select() extension method. from o in orders group o by o.Id  Groups output same as the GroupBy() extension method.
  • 34. Query Syntax In between the from and the select/group clauses, you can filter, order, and join as desired: where – filters to only matching objects, like Where(). into – allows storage of results from a groupby, join, or select for use in later clauses. order – sorts the results, can use ascending or descending modifiers, like the OrderBy() method family. join – joins two sequences together, like Join(). let – allows storage of result from sub-expression for use in later clauses.
  • 35. Same Query, Different Syntax These two queries are the same, just different syntax:
  • 36. Pros and cons Pros: Lambda expressions are much more concise than full methods for simple tasks. LINQ expressions simplifies implementation by using .NET libraries that are optimized and fully tested. Once you learn the lambdas and LINQ, ultimately the code is much more readable and easier to maintain. Cons: The lambda syntax and LINQ methods take some getting used to at first (short ramp-up time).
  • 37. Summary Delegates help create more modular code without the need for inheritance. Lambdas are just an easy way to assign an anonymous method to a delegate. LINQ introduces a great class library for performing common operations on sequences of data. Using lambdas and LINQ can help reduce code size and improve code readability and maintainability. Care should be taken to make lambda expressions concise and meaningful while still readable.