• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Linq And Its Impact On The.Net Framework
 

Linq And Its Impact On The.Net Framework

on

  • 2,152 views

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

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

Statistics

Views

Total Views
2,152
Views on SlideShare
2,149
Embed Views
3

Actions

Likes
2
Downloads
0
Comments
0

1 Embed 3

http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Linq And Its Impact On The.Net Framework Linq And Its Impact On The.Net Framework Presentation Transcript

  • 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