LINQ and Its Impact on the .NET Framework Richard Rush
LINQ and Its Impact on the .NET Framework <ul><li>What is LINQ? </li></ul><ul><li>New features that enable LINQ </li></ul>...
What is LINQ? <ul><li>L anguage  IN tegrated  Q uery </li></ul><ul><li>Designed to provide a uniform way to retrieve data ...
New Features that Enable LINQ <ul><li>Extension methods </li></ul><ul><li>Lambda expressions </li></ul><ul><li>Anonymous c...
Extension Methods <ul><li>Add functionality to , or “extend,”  classes you  cannot alter the source code for. </li></ul><u...
Lambda Expressions <ul><li>Anonymous Methods </li></ul><ul><li>Work sort of like Delegates </li></ul><ul><li>Different not...
Anonymous Constructors <ul><li>Allow the initialization of objects and their settable properties in a single line </li></u...
Anonymous Constructor Example <ul><li>p  = new PersonObject() { Id = 1,  </li></ul><ul><li>  RoleId = 2,  </li></ul><ul><l...
Anonymous Fields <ul><li>Implicitly define a field by explicitly defining a property </li></ul>
Anonymous Field Example <ul><li>So </li></ul><ul><li>private  string  _FirstName; </li></ul><ul><li>public string  FirstNa...
<ul><li>So, now we have: </li></ul><ul><li>Anonymous methods (Lamba Expressions) </li></ul><ul><li>Anonymous constructors ...
Anonymous Types and Implicit Typing <ul><li>Bring all of the previous enhancements together </li></ul><ul><li>Define  stro...
Anonymous Type and Implicit Typing Examples <ul><li>var i = 5; </li></ul><ul><li>var s = “This is a string.”; </li></ul><u...
<ul><li>How does all of this relate to LINQ? </li></ul>
Some Assumptions… <ul><li>public class Person { </li></ul><ul><li>public string FirstName { get; set; }   </li></ul><ul><l...
Example: Object-Oriented Select <ul><li>public  List<Person>  SelectPerson(int id) {   </li></ul><ul><li>List<Person> outp...
Example: Object-Oriented Select with Extension Method <ul><ul><li>public  List<Person>  SelectPerson(int id) {   </li></ul...
Example: Object-Oriented LINQ Extension Method Use <ul><li>public IEnumerable<Person> SelectPerson(int id) {   </li></ul><...
Example: LINQ <ul><li>public IEnumerable<Person> SelectPerson(int id) {   </li></ul><ul><li>var query = from p in  _P eopl...
Example: Object-Oriented LINQ Extension Method Use and Anonymous Types <ul><li>public ?????? SelectPerson(int id) {   </li...
Example: LINQ with Anonymous Types <ul><li>public ?????? SelectPerson(int id) {   </li></ul><ul><li>var query = from p in ...
LINQ Providers <ul><li>LINQ to Objects </li></ul><ul><li>LINQ to XML </li></ul><ul><li>LINQ to DataSets </li></ul><ul><li>...
LINQ to XML <ul><li>public ?????? SelectPerson(int id) { </li></ul><ul><li>XDocument peopleDoc = XDocument.Load(Server.Map...
LINQ to DataSets <ul><li>public ?????? SelectPerson(int id) {   </li></ul><ul><li>PeopleDataSet  peopleD ata  =  DataAcces...
LINQ to SQL <ul><li>(we’re going to need to take a trip to VisualStudio 2008 for this) </li></ul>
Custom LINQ Providers <ul><li>LINQ to Amazon </li></ul><ul><li>LINQ to Google </li></ul><ul><li>LINQ to Google Desktop </l...
LINQ to Amazon <ul><li>public IEnumerable<AmazonBook> SelectBooks() {   </li></ul><ul><li>var query = from book in new Ama...
Summary <ul><li>Overview of LINQ </li></ul><ul><li>Language changes to facilitate LINQ </li></ul><ul><ul><li>Extension met...
Upcoming SlideShare
Loading in …5
×

Linq And Its Impact On The.Net Framework

1,628 views

Published on

A brief overview of LINQ and an analysis of the enhancements its introduction imposed on the .NET Framework

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,628
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Linq And Its Impact On The.Net Framework

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

    ×