SlideShare a Scribd company logo
LINQ 16/10/2008
Agenda Impedance Mismatch C# 3.0 New Features Introduction to LINQ LINQ to Objects LINQ to SQL LINQ to XML Summary
Impedance Mismatch Incompatibility between systems. Describes an inadequate ability of one system  to accommodate input from another.
Introduction RDBMS Business Logic Windows UI Web UI Console Web Service Windows Service Impedance Mismatch Data Access
C# 3.0 Features Implicitly Typed Local Variables Automatic Properties Object and Collection Initializers Anonymous Types Extension Methods Lambda Expressions LINQ
Implicitly Typed Local Variables The  var  keyword // C# 2.0 int x = 5; string name = &quot;Bart Simpson&quot;; Dictionary<string, object> data = new Dictionary<string, object>(); int size = name.Length; // C# 3.0 var x = 5; var name = &quot;Bart Simpson&quot;; var data = new Dictionary<string, object>(); var size = name.Length; var y = x; var keys = data.Keys; // Dictionary<string, object>.KeyCollection
Automatic Properties public class Person { // C# 2.0 private string _firstName, _lastName; private int _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public int Age { get { return _age; } set { _age = value; } } } public class Person { // C# 3.0 public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
Object Initializers // C# 2.0 Person p = new Person(); p.FirstName = &quot;Bart&quot;; p.LastName = &quot;Simpson&quot;; p.Age = 12; // C# 3.0 Person p = new Person() {  FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12  };
Collection Initializers // C# 3.0 List<Person> people = new List<Person>(); people.Add(new Person() {  FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12  }); people.Add(new Person() { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 35  }); people.Add(new Person() {  FirstName = &quot;Peter&quot;, LastName = &quot;Parker&quot;, Age = 30  }); // C# 3.0 var people = new List<Person>() { new Person() { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12 }, new Person() { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 35 }, new Person() { FirstName = &quot;Peter&quot;, LastName = &quot;Parker&quot;, Age = 30 } };
Anonymous Types var people = new[] { new { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 36 }, new { FirstName = &quot;Peter&quot;, LastName = &quot;parker&quot;, Age = 26 }, new { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 11 } }; foreach (var i in people) Console.WriteLine(&quot;{0} {1} ({2})&quot;, i.FirstName, i.LastName, i.Age); Console.WriteLine(people[0].GetType().FullName); // ??? In which the type name is generated by the compiler and Is not available at the source code level.
Extension Methods Extension methods allows us to “add” method to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of “static” method, but they are called as if they were instance methods. The first parameter specifies which type the method operates on, and the parameter is preceded by the “this” modifier.
Extension Methods public static class MyExtensions { public static string UpperLower( this  string str, bool upperFirst) { StringBuilder newString = new StringBuilder(str.Length); for (int i = 0; i < str.Length; i++) { newString.Append(upperFirst ? char.ToUpper(str[i]) : char.ToLower(str[i])); upperFirst = !upperFirst; } return newString.ToString(); } } string input = Console.ReadLine(); Console.WriteLine(&quot;calling extension method for {0}: {1}&quot;, input,  input. UpperLower (true));
Lambda Expressions A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.  var x => x+1;  //Implicitly typed expression. var x => { return x+1;} //Implicitly type statement. int x => x+1; //Explicitly typed expression. int x => {return x+1;} //Explicitly type  statement.
var prs = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20 * 1024 * 1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name = process.ProcessName }); Extension methods Local variable type inference Anonymous types Object initializers Lambda expressions Language Extensions
Introduction to LINQ What is LINQ? Stands for Language INtegrated Query Allows developers to query data structures using SQL-like syntax from within their application’s code Is available to both C# and Visual Basic developers in Visual Studio 2008
The LINQ Project C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
LINQ to Objects C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
LINQ to Object example LINQ Example - Querying an array //Create an array of integers int [] myarray =  new   int [] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 }; //Create a query for odd numbers var  oddNumbers =  from  i  in  myarray  where  i % 2 == 1 select  i; //Display the results of the query foreach  ( int  i  in  oddNumbers) Console .WriteLine(i); //Create a query for odd numbers, sorted var  oddNumbers =  from  i  in  myarray  where  i % 2 == 1 orderby  i select  i; //Create a query for odd numbers, sorted in descending order var  oddNumbers =  from  i  in  myarray  where  i % 2 == 1 orderby  i  descending  select  i; //Create a query for odd numbers var  oddNumbers =  from  i  in  myarray  where  i % 2 == 1  select  i; //Compose the original query to create a query for odd numbers var  sorted =  from  i  in  oddNumbers  orderby  i  descending select  i;
LINQ to Objects Native query syntax in C# and VB IntelliSense Autocompletion Query Operators can be used against any .NET collection (IEnumerable<T>) Select, Where, GroupBy, Join, etc. Deferred Query Evaluation Lambda Expressions using  System; using  System.Query; using  System.Collections.Generic;   class   app  { static void  Main() { string [] names = { &quot;Allen&quot;, &quot;Arthur&quot;,    &quot;Bennett&quot; };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach  ( string  item  in  ayes)  Console .WriteLine(item);   names[0] = &quot;Bob&quot;;   foreach  ( string  item  in  ayes)  Console .WriteLine(item); } } Arthur using  System; using  System.Query; using  System.Collections.Generic;   class   app  { static void  Main() { string [] names = { &quot;Allen&quot;, &quot;Arthur&quot;,    &quot;Bennett&quot; };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach  ( string  item  in  ayes)  Console .WriteLine(item);   names[0] = &quot;Bob&quot;;   foreach  ( string  item  in  ayes)  Console .WriteLine(item); } } Allen Arthur using  System; using  System.Query; using  System.Collections.Generic;   class   app  { static void  Main() { string [] names = { &quot;Burke&quot;, &quot;Connor&quot;,    &quot;Frank&quot;, &quot;Everett&quot;,      &quot;Albert&quot;, &quot;George&quot;,  &quot;Harris&quot;, &quot;David&quot; };   IEnumerable < string > expr =  from  s  in  names  where  s.Length == 5 orderby  s select  s.ToUpper();   foreach  ( string  item  in  expr) Console .WriteLine(item); } } BURKE DAVID FRANK using  System; using  System.Query; using  System.Collections.Generic;   class   app  { static void  Main() { string [] names = { &quot;Burke&quot;, &quot;Connor&quot;,    &quot;Frank&quot;, &quot;Everett&quot;,      &quot;Albert&quot;, &quot;George&quot;,  &quot;Harris&quot;, &quot;David&quot; };    Func < string ,  bool > filter = s => s.Length == 5; Func < string ,  string > extract = s => s; Func < string ,  string > project = s = s.ToUpper();   IEnumerable < string > expr = names .Where(filter)  .OrderBy(extract) .Select(project);   foreach  ( string  item  in  expr) Console .WriteLine(item); } } BURKE DAVID FRANK
LINQ to SQL C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
LINQ to SQL Architecture Enumerate SQL Query or SProc Rows Objects SubmitChanges() DML  or SProcs Application LINQ to SQL from c in db.Customers where c.City == &quot;London&quot; select c.CompanyName SELECT CompanyName FROM Customer WHERE City = 'London' db.Customers.Add(c1); c2.City = “Seattle&quot;; db.Customers.Remove(c3); INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer …
LINQ to SQL Maps .NET classes to relational SQL data Translates LINQ queries to SQL execution Supports change tracking for insert, update, delete operations Built on top of ADO.NET and integrates with connection-pooling and transactions
Querying SQL SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @&quot;SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0&quot;); cmd.Parameters[&quot;@p0&quot;] = &quot;London&quot;; DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = r.GetString(0); string phone = r.GetString(1); DateTime date = r.GetDateTime(2); } r.Close(); Accessing  relational  data today Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks
LINQ to SQL public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … } Northwind db = new Northwind(…); var contacts = from c in db.Customers where c.City == &quot;London&quot; select new { c.Name, c.Phone }; Accessing relational data with LINQ Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections
Language Extensions var contacts = from c in dc.GetTable<customers>() where c.State == &quot;WA&quot; select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == &quot;WA&quot;) .Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference
LINQ to XML C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
LINQ to XML Large Improvement Over Existing Model Declarative Model Supports: Creating XML Loading & querying XML Modifying & saving XML
LINQ to XML XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement(&quot;contacts&quot;); foreach (Customer c in customers) if (c.Country == &quot;USA&quot;) { XmlElement e = doc.CreateElement(&quot;contact&quot;); XmlElement name = doc.CreateElement(&quot;name&quot;); name.InnerText = c.CompanyName; e.AppendChild(name); XmlElement phone = doc.CreateElement(&quot;phone&quot;); phone.InnerText = c.Phone; e.AppendChild(phone); contacts.AppendChild(e); } doc.AppendChild(contacts); Programming XML today <contacts> <contact> <name>Great Lakes Food</name> <phone>(503) 555-7123</phone> </contact> … </contacts> Imperative model Document centric No integrated queries Memory intensive
LINQ to XML XElement contacts = new XElement(&quot;contacts&quot;, from c in customers where c.Country == &quot;USA&quot; select new XElement(&quot;contact&quot;, new XElement(&quot;name&quot;, c.CompanyName), new XElement(&quot;phone&quot;, c.Phone) ) ); Programming XML with LINQ Declarative model Element centric Integrated queries Smaller and faster
Summary LINQ will be a really important technology. Native query integration within languages improves productivity and error checking. Using LINQ with ASP.NET is both easy and fun. ASP.Net + LINQ - a simple and powerful way to build data driven web applications.
THANK YOU

More Related Content

PDF
Amusing C#
PDF
CS4200 2019 | Lecture 2 | syntax-definition
PPT
Antlr V3
PPT
Csharp In Detail Part2
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
PPTX
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
PPT
Amusing C#
CS4200 2019 | Lecture 2 | syntax-definition
Antlr V3
Csharp In Detail Part2
CS4200 2019 | Lecture 4 | Syntactic Services
Lecture 3, c++(complete reference,herbet sheidt)chapter-13

What's hot (19)

PPS
C++ Language
PPT
devLink - What's New in C# 4?
PDF
Why Java Sucks and C# Rocks (Final)
PDF
C++11 concurrency
PPTX
02. Primitive Data Types and Variables
PDF
Pipeline oriented programming
PPTX
Python programming workshop session 1
PDF
Get Functional on the CLR: Intro to Functional Programming with F#
PPTX
04. Console Input Output
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Core csharp and net quick reference
PDF
Functional Programming in F#
PPTX
C++ 11 Features
PPT
JAVA Variables and Operators
PPTX
Introduction of flex
PDF
Modern c++ (C++ 11/14)
PDF
C# Cheat Sheet
PDF
Writing Parsers and Compilers with PLY
PPTX
C++ theory
C++ Language
devLink - What's New in C# 4?
Why Java Sucks and C# Rocks (Final)
C++11 concurrency
02. Primitive Data Types and Variables
Pipeline oriented programming
Python programming workshop session 1
Get Functional on the CLR: Intro to Functional Programming with F#
04. Console Input Output
Compiler Construction | Lecture 8 | Type Constraints
Core csharp and net quick reference
Functional Programming in F#
C++ 11 Features
JAVA Variables and Operators
Introduction of flex
Modern c++ (C++ 11/14)
C# Cheat Sheet
Writing Parsers and Compilers with PLY
C++ theory
Ad

Viewers also liked (12)

PPT
Introduction to Linq
PPTX
C# linq
PPTX
Hipaa
PPTX
Pharmacy
DOCX
Operating system
PDF
Communique English Final Tal
PPTX
Transcutaneous bilirubin monitoring (bilichek)
PPT
Linq 090701233237 Phpapp01
PPT
PPT
Understanding linq
PPTX
Hand hygiene
Introduction to Linq
C# linq
Hipaa
Pharmacy
Operating system
Communique English Final Tal
Transcutaneous bilirubin monitoring (bilichek)
Linq 090701233237 Phpapp01
Understanding linq
Hand hygiene
Ad

Similar to PostThis (20)

PPT
Linq intro
PPTX
PPT
Linq
PPT
Linq in C# 3.0: An Overview
PDF
C# Starter L05-LINQ
PPT
Linq
PPT
Linq
PDF
Beginning linq
PPTX
Think in linq
PPT
Linq 1224887336792847 9
PPT
PPT
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
PPTX
COLLECTIONS.pptx
PPT
Linq To The Enterprise
PPT
Whats New In C# 3.0
PPT
Language Integrated Query By Nyros Developer
PPT
C#3.0 & Vb 9.0 New Features
PPTX
SQL Saturday 28 - .NET Fundamentals
PPTX
Linq Introduction
PDF
Litwin linq
Linq intro
Linq
Linq in C# 3.0: An Overview
C# Starter L05-LINQ
Linq
Linq
Beginning linq
Think in linq
Linq 1224887336792847 9
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
COLLECTIONS.pptx
Linq To The Enterprise
Whats New In C# 3.0
Language Integrated Query By Nyros Developer
C#3.0 & Vb 9.0 New Features
SQL Saturday 28 - .NET Fundamentals
Linq Introduction
Litwin linq

PostThis

  • 2. Agenda Impedance Mismatch C# 3.0 New Features Introduction to LINQ LINQ to Objects LINQ to SQL LINQ to XML Summary
  • 3. Impedance Mismatch Incompatibility between systems. Describes an inadequate ability of one system to accommodate input from another.
  • 4. Introduction RDBMS Business Logic Windows UI Web UI Console Web Service Windows Service Impedance Mismatch Data Access
  • 5. C# 3.0 Features Implicitly Typed Local Variables Automatic Properties Object and Collection Initializers Anonymous Types Extension Methods Lambda Expressions LINQ
  • 6. Implicitly Typed Local Variables The var keyword // C# 2.0 int x = 5; string name = &quot;Bart Simpson&quot;; Dictionary<string, object> data = new Dictionary<string, object>(); int size = name.Length; // C# 3.0 var x = 5; var name = &quot;Bart Simpson&quot;; var data = new Dictionary<string, object>(); var size = name.Length; var y = x; var keys = data.Keys; // Dictionary<string, object>.KeyCollection
  • 7. Automatic Properties public class Person { // C# 2.0 private string _firstName, _lastName; private int _age; public string FirstName { get { return _firstName; } set { _firstName = value; } } public string LastName { get { return _lastName; } set { _lastName = value; } } public int Age { get { return _age; } set { _age = value; } } } public class Person { // C# 3.0 public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
  • 8. Object Initializers // C# 2.0 Person p = new Person(); p.FirstName = &quot;Bart&quot;; p.LastName = &quot;Simpson&quot;; p.Age = 12; // C# 3.0 Person p = new Person() { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12 };
  • 9. Collection Initializers // C# 3.0 List<Person> people = new List<Person>(); people.Add(new Person() { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12 }); people.Add(new Person() { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 35 }); people.Add(new Person() { FirstName = &quot;Peter&quot;, LastName = &quot;Parker&quot;, Age = 30 }); // C# 3.0 var people = new List<Person>() { new Person() { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 12 }, new Person() { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 35 }, new Person() { FirstName = &quot;Peter&quot;, LastName = &quot;Parker&quot;, Age = 30 } };
  • 10. Anonymous Types var people = new[] { new { FirstName = &quot;Clark&quot;, LastName = &quot;Kent&quot;, Age = 36 }, new { FirstName = &quot;Peter&quot;, LastName = &quot;parker&quot;, Age = 26 }, new { FirstName = &quot;Bart&quot;, LastName = &quot;Simpson&quot;, Age = 11 } }; foreach (var i in people) Console.WriteLine(&quot;{0} {1} ({2})&quot;, i.FirstName, i.LastName, i.Age); Console.WriteLine(people[0].GetType().FullName); // ??? In which the type name is generated by the compiler and Is not available at the source code level.
  • 11. Extension Methods Extension methods allows us to “add” method to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of “static” method, but they are called as if they were instance methods. The first parameter specifies which type the method operates on, and the parameter is preceded by the “this” modifier.
  • 12. Extension Methods public static class MyExtensions { public static string UpperLower( this string str, bool upperFirst) { StringBuilder newString = new StringBuilder(str.Length); for (int i = 0; i < str.Length; i++) { newString.Append(upperFirst ? char.ToUpper(str[i]) : char.ToLower(str[i])); upperFirst = !upperFirst; } return newString.ToString(); } } string input = Console.ReadLine(); Console.WriteLine(&quot;calling extension method for {0}: {1}&quot;, input, input. UpperLower (true));
  • 13. Lambda Expressions A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types. var x => x+1; //Implicitly typed expression. var x => { return x+1;} //Implicitly type statement. int x => x+1; //Explicitly typed expression. int x => {return x+1;} //Explicitly type statement.
  • 14. var prs = Process.GetProcesses() .Where(process => process.WorkingSet64 > 20 * 1024 * 1024) .OrderByDescending(process => process.WorkingSet64) .Select(process => new { process.Id, Name = process.ProcessName }); Extension methods Local variable type inference Anonymous types Object initializers Lambda expressions Language Extensions
  • 15. Introduction to LINQ What is LINQ? Stands for Language INtegrated Query Allows developers to query data structures using SQL-like syntax from within their application’s code Is available to both C# and Visual Basic developers in Visual Studio 2008
  • 16. The LINQ Project C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
  • 17. LINQ to Objects C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
  • 18. LINQ to Object example LINQ Example - Querying an array //Create an array of integers int [] myarray = new int [] { 49, 28, 20, 15, 25, 23, 24, 10, 7, 34 }; //Create a query for odd numbers var oddNumbers = from i in myarray where i % 2 == 1 select i; //Display the results of the query foreach ( int i in oddNumbers) Console .WriteLine(i); //Create a query for odd numbers, sorted var oddNumbers = from i in myarray where i % 2 == 1 orderby i select i; //Create a query for odd numbers, sorted in descending order var oddNumbers = from i in myarray where i % 2 == 1 orderby i descending select i; //Create a query for odd numbers var oddNumbers = from i in myarray where i % 2 == 1 select i; //Compose the original query to create a query for odd numbers var sorted = from i in oddNumbers orderby i descending select i;
  • 19. LINQ to Objects Native query syntax in C# and VB IntelliSense Autocompletion Query Operators can be used against any .NET collection (IEnumerable<T>) Select, Where, GroupBy, Join, etc. Deferred Query Evaluation Lambda Expressions using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { &quot;Allen&quot;, &quot;Arthur&quot;, &quot;Bennett&quot; };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach ( string item in ayes) Console .WriteLine(item);   names[0] = &quot;Bob&quot;;   foreach ( string item in ayes) Console .WriteLine(item); } } Arthur using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { &quot;Allen&quot;, &quot;Arthur&quot;, &quot;Bennett&quot; };   IEnumerable < string > ayes = names .Where(s => s[0] == 'A');   foreach ( string item in ayes) Console .WriteLine(item);   names[0] = &quot;Bob&quot;;   foreach ( string item in ayes) Console .WriteLine(item); } } Allen Arthur using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { &quot;Burke&quot;, &quot;Connor&quot;, &quot;Frank&quot;, &quot;Everett&quot;, &quot;Albert&quot;, &quot;George&quot;, &quot;Harris&quot;, &quot;David&quot; };   IEnumerable < string > expr = from s in names where s.Length == 5 orderby s select s.ToUpper();   foreach ( string item in expr) Console .WriteLine(item); } } BURKE DAVID FRANK using System; using System.Query; using System.Collections.Generic;   class app { static void Main() { string [] names = { &quot;Burke&quot;, &quot;Connor&quot;, &quot;Frank&quot;, &quot;Everett&quot;, &quot;Albert&quot;, &quot;George&quot;, &quot;Harris&quot;, &quot;David&quot; };   Func < string , bool > filter = s => s.Length == 5; Func < string , string > extract = s => s; Func < string , string > project = s = s.ToUpper();   IEnumerable < string > expr = names .Where(filter) .OrderBy(extract) .Select(project);   foreach ( string item in expr) Console .WriteLine(item); } } BURKE DAVID FRANK
  • 20. LINQ to SQL C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
  • 21. LINQ to SQL Architecture Enumerate SQL Query or SProc Rows Objects SubmitChanges() DML or SProcs Application LINQ to SQL from c in db.Customers where c.City == &quot;London&quot; select c.CompanyName SELECT CompanyName FROM Customer WHERE City = 'London' db.Customers.Add(c1); c2.City = “Seattle&quot;; db.Customers.Remove(c3); INSERT INTO Customer … UPDATE Customer … DELETE FROM Customer …
  • 22. LINQ to SQL Maps .NET classes to relational SQL data Translates LINQ queries to SQL execution Supports change tracking for insert, update, delete operations Built on top of ADO.NET and integrates with connection-pooling and transactions
  • 23. Querying SQL SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new SqlCommand( @&quot;SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0&quot;); cmd.Parameters[&quot;@p0&quot;] = &quot;London&quot;; DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = r.GetString(0); string phone = r.GetString(1); DateTime date = r.GetDateTime(2); } r.Close(); Accessing relational data today Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks
  • 24. LINQ to SQL public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … } Northwind db = new Northwind(…); var contacts = from c in db.Customers where c.City == &quot;London&quot; select new { c.Name, c.Phone }; Accessing relational data with LINQ Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections
  • 25. Language Extensions var contacts = from c in dc.GetTable<customers>() where c.State == &quot;WA&quot; select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == &quot;WA&quot;) .Select(c => new { c.Name, c.Phone }); Extension methods Lambda expressions Query expressions Object initializers Anonymous types Local variable type inference
  • 26. LINQ to XML C# 3.0 Visual Basic 9.0 Others .NET Language Integrated Query
  • 27. LINQ to XML Large Improvement Over Existing Model Declarative Model Supports: Creating XML Loading & querying XML Modifying & saving XML
  • 28. LINQ to XML XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement(&quot;contacts&quot;); foreach (Customer c in customers) if (c.Country == &quot;USA&quot;) { XmlElement e = doc.CreateElement(&quot;contact&quot;); XmlElement name = doc.CreateElement(&quot;name&quot;); name.InnerText = c.CompanyName; e.AppendChild(name); XmlElement phone = doc.CreateElement(&quot;phone&quot;); phone.InnerText = c.Phone; e.AppendChild(phone); contacts.AppendChild(e); } doc.AppendChild(contacts); Programming XML today <contacts> <contact> <name>Great Lakes Food</name> <phone>(503) 555-7123</phone> </contact> … </contacts> Imperative model Document centric No integrated queries Memory intensive
  • 29. LINQ to XML XElement contacts = new XElement(&quot;contacts&quot;, from c in customers where c.Country == &quot;USA&quot; select new XElement(&quot;contact&quot;, new XElement(&quot;name&quot;, c.CompanyName), new XElement(&quot;phone&quot;, c.Phone) ) ); Programming XML with LINQ Declarative model Element centric Integrated queries Smaller and faster
  • 30. Summary LINQ will be a really important technology. Native query integration within languages improves productivity and error checking. Using LINQ with ASP.NET is both easy and fun. ASP.Net + LINQ - a simple and powerful way to build data driven web applications.

Editor's Notes

  • #5: From time immemorial (i.e. from 1990 th ) people use Relational DBMS for data storing and Object Oriented language for data behavior implementation. RDBMS stores and processes quickly large amounts of data by. And Object Oriented code manages them by clear and well-supportable way. In enterprise applications the Business Functionality Tier is a central component. As a rule it contains a set of entry points, that are exploited by other components. These components use the business functionality tier in various ways. Some performs a OrderItem of short operations, initiated interactively. Response time of these operations must be appropriate. Others performs large work like data import or transferring data to other applications. Independently on their type, the components use the same functionality, coded inside business functionality tier. The problem is that the functionality that is intended for interactive components, works slowly for B2B components. This effect is caused by well-known problem between object oriented and relational point of view. The problem and has name Impedance Mismatch. Data are represented variously in object code and in relational data store. Business functionality level sees data in form of object tree, while relational data storage represents them in form of tables. This difference mitigated by special Data Access Level, that contains automatically generated objects for all business entities represented in the database and provides framework that can load and synchronize them with database. There are a couple ORM tools that support this functionality. But as soon as relational records are wrapped into objects, performance of thee application slow down. Because business functionality level sends them to database record by record, separately. At the same time we know that relational DBMS can process large amounts of data really quickly if only they are presented in form of relational tables, The problem become critical firstly in B2B part of the enterprise application. Standard solution that is commonly used is replacing of object-oriented pieces of code with pieces of clear SQL code. Or creation of stored procedures and calling them directly passing the business functionality level. The performance problem goes away, but other problem appears. Application starts to contain twice coded functionality. Both branches must work equally. So when the functionality is changed, developers are obliged not to forget to change it in both branches and then test the sameness of the branches. Two different approaches This causes significant overhead in development and support processes.
  • #23: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #24: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #25: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #26: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #29: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #30: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • #31: 12/03/09 12:15 © 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.