SlideShare a Scribd company logo
LINQ and Its Impact on the .NET Framework Richard Rush
LINQ and Its Impact on the .NET Framework What is LINQ? New features that enable LINQ LINQ providers Examples
What is LINQ? L anguage  IN tegrated  Q uery Designed to provide a uniform way to retrieve data from different types of data stores Can query against anything that implements the  IEnumerable<T>  interface
New Features that Enable LINQ Extension methods Lambda expressions Anonymous constructors Anonymous fields Anonymous types and implicit typing
Extension Methods Add functionality to , or “extend,”  classes you  cannot alter the source code for. Every class can be extended public static  [output]   [Extension] (this  [class]  source) { // Method Body } public static string HtmlEncode(this string source) { return System.Web.HttpUtility.HtmlEncode(source); }
Lambda Expressions Anonymous Methods Work sort of like Delegates Different notation for functions Defined as (λ V. E), where: V is an identifier E is an operation, or another lambda expression x => x+2 .NET Lamba Notation λ  x .  x +2  Lamba Notation f ( x ) =  x +2  Standard Notation
Anonymous Constructors Allow the initialization of objects and their settable properties in a single line Calls the constructor and then sets all the specified properties to the specified values No constructor overload is needed, but they can still be used Anonymous constructors can be nested
Anonymous Constructor Example p  = new PersonObject() { Id = 1,    RoleId = 2,    FirstName = &quot;Peter&quot;,    LastName = &quot;Parker&quot; };   Is equivalent to: p = new PersonObject(); p.Id = 1; p.RoleId = 2; p.FirstName = “Peter”; p.LastName = “Parker”;
Anonymous Fields Implicitly define a field by explicitly defining a property
Anonymous Field Example So private  string  _FirstName; public string  FirstName {  get  { return _FirstName; } set  { _FirstName = value; } } and public  string  FirstName; become public string FirstName { get; set; }
So, now we have: Anonymous methods (Lamba Expressions) Anonymous constructors Anonymous fields What could possibly be next?
Anonymous Types and Implicit Typing Bring all of the previous enhancements together Define  strongly typed  classes implicitly Supported by Intellisense Locally scoped
Anonymous Type and Implicit Typing Examples var i = 5; var s = “This is a string.”; var p = new { FirstName=“Peter”, LastName=“Parker”} var x;  // This won’t work.  Exception time! i = “Well, what about this?”;  // Nope!
How does all of this relate to LINQ?
Some Assumptions… public class Person { public string FirstName { get; set; }   public int Id   { get; set; }   public int RoleId { get; set; }   public string LastName { get; set; }   public Person () { } public Person (int id) { Id = id; } } List<Person> _PeopleList = FillPeopleList();
Example: Object-Oriented Select public  List<Person>  SelectPerson(int id) {   List<Person> output = new List<Person>(); foreach (Person currentPerson in  _P eopleList) { if (currentPerson.Id ==  id ) { output.Add(currentPerson); } } } return output; }
Example: Object-Oriented Select with Extension Method public  List<Person>  SelectPerson(int id) {   return _P eopleList .Where(id); } public static  List<Person>   Where (this  List<Person>  source ,  int id ) { List<Person> output = new List<Person>(); foreach (Person currentPerson in  source ) { if (currentPerson.Id ==  id ) { output.Add(currentPerson); } } return  output; }
Example: Object-Oriented LINQ Extension Method Use public IEnumerable<Person> SelectPerson(int id) {   var query =  _P eopleList.Where(p => p.Id == inputId); return  query ; }
Example: LINQ public IEnumerable<Person> SelectPerson(int id) {   var query = from p in  _P eopleList where p.Id == inputId select p; return  query ; }
Example: Object-Oriented LINQ Extension Method Use and Anonymous Types public ?????? SelectPerson(int id) {   var query =  _P eopleList .Where(p => p.Id == inputId) .Select(p=> new {  p.FirstName, p.LastName  }) ; return  query ; }
Example: LINQ with Anonymous Types public ?????? SelectPerson(int id) {   var query = from p in  _P eopleList where p.Id == inputId select new { p.FirstName, p.LastName }; return  query ; }
LINQ Providers LINQ to Objects LINQ to XML LINQ to DataSets LINQ to SQL Custom LINQ Providers
LINQ to XML public ?????? SelectPerson(int id) { XDocument peopleDoc = XDocument.Load(Server.MapPath(&quot;~/App_Data/People.xml&quot;)); var query =  from p in   peopleDoc.Elements(&quot;People&quot;).Elements(&quot;Person&quot;) where p.Element(&quot;Id&quot;).Value == id select new   { FirstName = p.Element(&quot;FirstName&quot;).Value, LastName = p.Element(&quot;LastName&quot;).Value }; return query; }
LINQ to DataSets public ?????? SelectPerson(int id) {   PeopleDataSet  peopleD ata  =  DataAccess.GetDataSet<PeopleDataSet>(&quot;GetData&quot;,  CommandType.StoredProcedure, tableMappings); var query = from p in  peopleData .Person where p.ID == inputId select new { p.FirstName, p.LastName }; return  query ; }
LINQ to SQL (we’re going to need to take a trip to VisualStudio 2008 for this)
Custom LINQ Providers LINQ to Amazon LINQ to Google LINQ to Google Desktop LINQ to Flickr
LINQ to Amazon public IEnumerable<AmazonBook> SelectBooks() {   var query = from book in new Amazon.BookSearch()    where (         book.Title.Contains(“Spider-Man&quot;) &&    (book.Publisher == “Lee&quot;) &&    (book.Price <= 25) &&       (book.Condition == BookCondition.New) )     select book;  return  query ; }
Summary Overview of LINQ Language changes to facilitate LINQ Extension methods Lambda Expressions Anonymous constructors Anonymous fields Anonymous types and implicit typing LINQ Providers LINQ to Objects LINQ to XML LINQ to SQL

More Related Content

PPTX
Linq Introduction
PPTX
Python dictionary
PDF
Linked to ArrayList: the full story
PPTX
Kotlin as a Better Java
PPTX
C# 7.0 Hacks and Features
PPT
C++ Returning Objects
PDF
Functional Java 8 in everyday life
PPTX
Chapter 16 Dictionaries
Linq Introduction
Python dictionary
Linked to ArrayList: the full story
Kotlin as a Better Java
C# 7.0 Hacks and Features
C++ Returning Objects
Functional Java 8 in everyday life
Chapter 16 Dictionaries

What's hot (20)

PPTX
PPTX
Linq and lambda
PDF
Head First Java Chapter 4
PDF
Head First Java Chapter 8
PDF
Intake 37 linq1
ODP
Functions & closures
PPTX
Python data type
PDF
Practical Functional Programming Presentation by Bogdan Hodorog
PPT
PostThis
ODP
Functional Programming With Scala
PPT
Linq intro
PPTX
The JavaScript Programming Language
PDF
Ti1220 Lecture 7: Polymorphism
PPTX
Dictionaries
PPT
Polymorphism
PDF
Data translation with SPARQL 1.1
PPTX
Function overloading
PPTX
Linq Sanjay Vyas
PPTX
Function overloading and overriding
PPTX
Namespace in C++ Programming Language
Linq and lambda
Head First Java Chapter 4
Head First Java Chapter 8
Intake 37 linq1
Functions & closures
Python data type
Practical Functional Programming Presentation by Bogdan Hodorog
PostThis
Functional Programming With Scala
Linq intro
The JavaScript Programming Language
Ti1220 Lecture 7: Polymorphism
Dictionaries
Polymorphism
Data translation with SPARQL 1.1
Function overloading
Linq Sanjay Vyas
Function overloading and overriding
Namespace in C++ Programming Language
Ad

Viewers also liked (6)

PPT
PDF
Humor 20i0 spring collection
PPTX
Humor 20i0 summer collection
PPS
The Tale Of Metal Fire
PPTX
Jaquizzi 4
PPT
Jaquizzi-6
Humor 20i0 spring collection
Humor 20i0 summer collection
The Tale Of Metal Fire
Jaquizzi 4
Jaquizzi-6
Ad

Similar to Linq And Its Impact On The.Net Framework (20)

PPT
Presentation
PDF
Grammarware Memes
PPTX
Lambda expressions
PDF
Domain Driven Design with the F# type System -- NDC London 2013
ODP
Scala introduction
PPT
Embedded Typesafe Domain Specific Languages for Java
PPT
IntroToCSharpcode.ppt
PPT
Mixing functional and object oriented approaches to programming in C#
PPT
Mixing Functional and Object Oriented Approaches to Programming in C#
PPT
강의자료10
PPTX
New C# features
PPT
Micro-ORM Introduction - Don't overcomplicate
PPTX
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
PPTX
Java Generics
PDF
Using C++ use a linked list to create an alphabetical Conta.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PPT
Re Descubriendo A Linq
ODP
2.1 Recap From Day One
PPSX
Tuga it 2016 - What's New In C# 6
Presentation
Grammarware Memes
Lambda expressions
Domain Driven Design with the F# type System -- NDC London 2013
Scala introduction
Embedded Typesafe Domain Specific Languages for Java
IntroToCSharpcode.ppt
Mixing functional and object oriented approaches to programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
강의자료10
New C# features
Micro-ORM Introduction - Don't overcomplicate
Domain Driven Design with the F# type System -- F#unctional Londoners 2014
Java Generics
Using C++ use a linked list to create an alphabetical Conta.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Re Descubriendo A Linq
2.1 Recap From Day One
Tuga it 2016 - What's New In C# 6

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Machine Learning_overview_presentation.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Network Security Unit 5.pdf for BCA BBA.
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Machine Learning_overview_presentation.pptx
sap open course for s4hana steps from ECC to s4
Mobile App Security Testing_ A Comprehensive Guide.pdf
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Spectral efficient network and resource selection model in 5G networks
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation_ Review paper, used for researhc scholars
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding

Linq And Its Impact On The.Net Framework

  • 1. LINQ and Its Impact on the .NET Framework Richard Rush
  • 2. LINQ and Its Impact on the .NET Framework What is LINQ? New features that enable LINQ LINQ providers Examples
  • 3. What is LINQ? L anguage IN tegrated Q uery Designed to provide a uniform way to retrieve data from different types of data stores Can query against anything that implements the IEnumerable<T> interface
  • 4. New Features that Enable LINQ Extension methods Lambda expressions Anonymous constructors Anonymous fields Anonymous types and implicit typing
  • 5. Extension Methods Add functionality to , or “extend,” classes you cannot alter the source code for. Every class can be extended public static [output] [Extension] (this [class] source) { // Method Body } public static string HtmlEncode(this string source) { return System.Web.HttpUtility.HtmlEncode(source); }
  • 6. Lambda Expressions Anonymous Methods Work sort of like Delegates Different notation for functions Defined as (λ V. E), where: V is an identifier E is an operation, or another lambda expression x => x+2 .NET Lamba Notation λ x . x +2 Lamba Notation f ( x ) = x +2 Standard Notation
  • 7. Anonymous Constructors Allow the initialization of objects and their settable properties in a single line Calls the constructor and then sets all the specified properties to the specified values No constructor overload is needed, but they can still be used Anonymous constructors can be nested
  • 8. Anonymous Constructor Example p = new PersonObject() { Id = 1, RoleId = 2, FirstName = &quot;Peter&quot;, LastName = &quot;Parker&quot; }; Is equivalent to: p = new PersonObject(); p.Id = 1; p.RoleId = 2; p.FirstName = “Peter”; p.LastName = “Parker”;
  • 9. Anonymous Fields Implicitly define a field by explicitly defining a property
  • 10. Anonymous Field Example So private string _FirstName; public string FirstName { get { return _FirstName; } set { _FirstName = value; } } and public string FirstName; become public string FirstName { get; set; }
  • 11. So, now we have: Anonymous methods (Lamba Expressions) Anonymous constructors Anonymous fields What could possibly be next?
  • 12. Anonymous Types and Implicit Typing Bring all of the previous enhancements together Define strongly typed classes implicitly Supported by Intellisense Locally scoped
  • 13. Anonymous Type and Implicit Typing Examples var i = 5; var s = “This is a string.”; var p = new { FirstName=“Peter”, LastName=“Parker”} var x; // This won’t work. Exception time! i = “Well, what about this?”; // Nope!
  • 14. How does all of this relate to LINQ?
  • 15. Some Assumptions… public class Person { public string FirstName { get; set; } public int Id { get; set; } public int RoleId { get; set; } public string LastName { get; set; } public Person () { } public Person (int id) { Id = id; } } List<Person> _PeopleList = FillPeopleList();
  • 16. Example: Object-Oriented Select public List<Person> SelectPerson(int id) { List<Person> output = new List<Person>(); foreach (Person currentPerson in _P eopleList) { if (currentPerson.Id == id ) { output.Add(currentPerson); } } } return output; }
  • 17. Example: Object-Oriented Select with Extension Method public List<Person> SelectPerson(int id) { return _P eopleList .Where(id); } public static List<Person> Where (this List<Person> source , int id ) { List<Person> output = new List<Person>(); foreach (Person currentPerson in source ) { if (currentPerson.Id == id ) { output.Add(currentPerson); } } return output; }
  • 18. Example: Object-Oriented LINQ Extension Method Use public IEnumerable<Person> SelectPerson(int id) { var query = _P eopleList.Where(p => p.Id == inputId); return query ; }
  • 19. Example: LINQ public IEnumerable<Person> SelectPerson(int id) { var query = from p in _P eopleList where p.Id == inputId select p; return query ; }
  • 20. Example: Object-Oriented LINQ Extension Method Use and Anonymous Types public ?????? SelectPerson(int id) { var query = _P eopleList .Where(p => p.Id == inputId) .Select(p=> new { p.FirstName, p.LastName }) ; return query ; }
  • 21. Example: LINQ with Anonymous Types public ?????? SelectPerson(int id) { var query = from p in _P eopleList where p.Id == inputId select new { p.FirstName, p.LastName }; return query ; }
  • 22. LINQ Providers LINQ to Objects LINQ to XML LINQ to DataSets LINQ to SQL Custom LINQ Providers
  • 23. LINQ to XML public ?????? SelectPerson(int id) { XDocument peopleDoc = XDocument.Load(Server.MapPath(&quot;~/App_Data/People.xml&quot;)); var query = from p in peopleDoc.Elements(&quot;People&quot;).Elements(&quot;Person&quot;) where p.Element(&quot;Id&quot;).Value == id select new { FirstName = p.Element(&quot;FirstName&quot;).Value, LastName = p.Element(&quot;LastName&quot;).Value }; return query; }
  • 24. LINQ to DataSets public ?????? SelectPerson(int id) { PeopleDataSet peopleD ata = DataAccess.GetDataSet<PeopleDataSet>(&quot;GetData&quot;, CommandType.StoredProcedure, tableMappings); var query = from p in peopleData .Person where p.ID == inputId select new { p.FirstName, p.LastName }; return query ; }
  • 25. LINQ to SQL (we’re going to need to take a trip to VisualStudio 2008 for this)
  • 26. Custom LINQ Providers LINQ to Amazon LINQ to Google LINQ to Google Desktop LINQ to Flickr
  • 27. LINQ to Amazon public IEnumerable<AmazonBook> SelectBooks() { var query = from book in new Amazon.BookSearch()   where (     book.Title.Contains(“Spider-Man&quot;) &&    (book.Publisher == “Lee&quot;) &&    (book.Price <= 25) &&     (book.Condition == BookCondition.New) )   select book; return query ; }
  • 28. Summary Overview of LINQ Language changes to facilitate LINQ Extension methods Lambda Expressions Anonymous constructors Anonymous fields Anonymous types and implicit typing LINQ Providers LINQ to Objects LINQ to XML LINQ to SQL