introduction to linq and lambda expressions
Download
Skip this Video
Download Presentation
Introduction to Linq and Lambda expressions

Loading in 2 Seconds...

play fullscreen
1 / 29

Introduction to Linq and Lambda expressions - PowerPoint PPT Presentation


  • 135 Views
  • Uploaded on

Introduction to Linq and Lambda expressions. Ir Denis VOITURON http://www.dvoituron.be. Denis Voituron. Support and consulting for software architects and developers Architectural Consulting and Prototyping Developer Coaching and Training

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introduction to Linq and Lambda expressions' - omar


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to linq and lambda expressions

Introduction to Linq and Lambda expressions

Ir Denis VOITURON

http://www.dvoituron.be

denis voituron
Denis Voituron
  • Support and consulting for software architects and developers
    • Architectural Consulting and Prototyping
    • Developer Coaching and Training
    • Application Optimization, Troubleshooting, Debugging
    • Architecture and Code Reviews
  • Webcasts, Slides & Samples

Introduction to Linq and Lambda expressions

agenda
Agenda
  • .NET evolution
  • Simplify Querying
  • LINQ
    • Local Variable Type Inference
    • Object Initialisers
    • Anonymous Types
    • Anonymous Methods
    • Lambda Expressions
    • Extension Methods
    • Query Expressions
  • Démonstrations

Introduction to Linq and Lambda expressions

net through the ages
.NET Through The Ages

Partial

Generic

WPFWCFWF

Lambda

Linq

Dynamic

+ … Classes … +

Introduction to Linq and Lambda expressions

the evolution of c
The Evolution of C#

Asyncrony

Dynamic prog.

Linq

Generics

Managed Code

Introduction to Linq and Lambda expressions

slide6

DEMO

Simplify Querying

slide7
Goal

C#

from c inCustomers

wherec.City == "Hove"

selectnew { c.Name, c.Address};

VB

Fromc In Customers _

Wherec.City= "Hove" _

Selectc.Name, c.Address

Introduction to Linq and Lambda expressions

the syntax
The Syntax

Starts with from

Zero or more from, join, let, where, or orderby

fromidinsource

{ fromidinsource |

joinidinsourceonexprequalsexpr [ intoid ] |

letid = expr |

wherecondition |

orderbyordering, ordering, … }

selectexpr | groupexprbykey

[ intoidquery ]

Ends with

selector group by

Optional into continuation

Introduction to Linq and Lambda expressions

language integrated query linq

LINQ enabled data sources

Language INtegrated Query (LINQ)

Others…

C#

VB

.NET Language-Integrated Query

LINQ enabled ADO.NET

LINQ

To XML

LINQ

To DataSets

LINQ

To Entities

LINQ

To Objects

LINQ

To SQL

<book>

<title/>

<author/>

<price/>

</book>

Relational

Objects

XML

Introduction to Linq and Lambda expressions

slide10
LINQ
  • Language Integrated Query
    • Microsoft .NET Framework component that adds native data querying capabilities to .NET languages
    • LINQ defines a set of methods, lambda expressions and anonymous types.
    • Goals
      • Integrate objects, relational data, and XML
      • Increase conciseness of language
      • Simplify querying data

Introduction to Linq and Lambda expressions

slide11
LINQ

LINQ

Introduction to Linq and Lambda expressions

local variable type inference
Local Variable Type Inference

int i = 5;

string s = "Hello";

double d = 1.0;

int[] numbers = newint[] { 1, 2, 3 };

List<int> orders = newList<int>();

var i = 5;

vars = "Hello";

vard = 1.0;

varnumbers = newint[] { 1, 2, 3 };

varorders = newList<int>();

The compiler makes the type of the variable match the type of the right side of the assignment

Introduction to Linq and Lambda expressions

object initialisers
Object Initialisers

publicclassPoint

{

privateint x, y;

publicint X { get { return x; } set { x = value; } }

publicint Y { get { return y; } set { y = value; } }

}

Point a = newPoint { X = 0, Y = 1 };

Point a = newPoint();

a.X = 0;

a.Y = 1;

Introduction to Linq and Lambda expressions

anonymous types
Anonymous types

var o = new { Name = "Jenny", Age = 31 };

classXXX

{

publicstring Name;

publicint Age;

}

The compiler create an associated class with correct properties.

Introduction to Linq and Lambda expressions

anonymous methods
Anonymous Methods

time.Elapsed += delegate(objectsender, ElapsedEventArgse)

{

// ...

};

time.Elapsed += newElapsedEventHandler(time_Elapsed);

private voidtime_Elapsed(object sender, ElapsedEventArgs e)

{

// ...

}

Anonymous methods allow declaration of inline methods without having to define a named method.

Introduction to Linq and Lambda expressions

lambda expressions
Lambda Expressions

publicdelegateboolPredicate<T>(T obj);

publicList<T> FindAll(Predicate<T> match)

publicint FindIndex(Predicate<T> match)

...

Predicates are simply generic Delegates which have been defined in .NET 2.0

Example

Introduction to Linq and Lambda expressions

lambda expressions1
Lambda Expressions

List<Person> people = Person.GetPeople();

List<Person> married = people.FindAll(

newPredicate<Person>( PersonIsMarried )

);

staticbool PersonIsMarried(Person p)

{

return p.IsMarried;

}

Predicate

Introduction to Linq and Lambda expressions

lambda expressions2
Lambda Expressions

List<Person> people = Person.GetPeople();

List<Person> married = people.FindAll(

delegate(Person p) {returnp.IsMarried;}

);

Anonymous Method

Introduction to Linq and Lambda expressions

lambda expressions3
Lambda Expressions

List<Person> people = Person.GetPeople();

List<Person> married = people.FindAll(

(Person p) => {returnp.IsMarried;}

);

Lambda Expression

Introduction to Linq and Lambda expressions

lambda expressions4
Lambda Expressions

List<Person> people = Person.GetPeople();

List<Person> married = people.FindAll(

p => p.IsMarried

);

Anonymous types

Introduction to Linq and Lambda expressions

extension methods
Extension Methods

publicstaticclassExtensions

{

publicstaticbool IsOver18(thisPerson p)

{

return p.Age >= 18;

}

}

List<Person> married = people.FindAll(

p => p.IsOver18()

);

Extend existing types with new methods

Introduction to Linq and Lambda expressions

slide22

DEMO

Local Variable Type Inference

Object Initialisers

Anonymous types

Anonymous Methods

Lambda Expressions

Extension Methods

query expressions
Query Expressions

from c in customers

where c.City== "Hove"

select new { c.Name, c.Phone };

customers

.Where(c => c.City== "Hove")

.Select(c => new { c.Name, c.Phone });

  • Queries translate to method invocations
    • Where, Join, OrderBy, Select, GroupBy, …

Introduction to Linq and Lambda expressions

query expressions1
Query Expressions

Introduction to Linq and Lambda expressions

slide25

DEMO

Bringing it all together andintroducing System.Linq

conclusion1
Conclusion

Query expressions

var contacts =

from c in customers

where c.City == "Hove"

selectnew { c.Name, c.Phone };

LINQ

Local variable type inference

Lambda expressions

var contacts =

customers

.Where(c => c.City == "Hove")

.Select(c => new { c.Name, c.Phone });

Extension methods

Anonymous types

Object initializers

Introduction to Linq and Lambda expressions

slide28

Thank You

Introduction to Linq and Lambda expressions

references
References
  • Daniel Moth\'s Blog
    • http://www.danielmoth.com/Blog
  • Microsoft 101 Linq Samples
    • http://msdn.microsoft.com/en-us/vcsharp/aa336746

Introduction to Linq and Lambda expressions

ad