Introduction to linq and lambda expressions
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Introduction to Linq and Lambda expressions PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Introduction to Linq and Lambda expressions

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

    • http://www.dvoituron.be

    • [email protected]

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


Introduction to linq and lambda expressions

DEMO

Simplify Querying


Introduction to linq and lambda expressions

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


Introduction to linq and lambda expressions

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


Introduction to linq and lambda expressions

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


Introduction to linq and lambda expressions

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


Introduction to linq and lambda expressions

DEMO

Bringing it all together andintroducing System.Linq


Conclusion

Conclusion


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


Introduction to linq and lambda expressions

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


  • Login