SlideShare una empresa de Scribd logo
1 de 31
LINQ 16/10/2008
Agenda ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Impedance Mismatch ,[object Object],[object Object],[object Object]
Introduction RDBMS Business Logic Windows UI Web UI Console Web Service Windows Service Impedance Mismatch Data Access
C# 3.0 Features ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Implicitly Typed Local Variables ,[object Object],// 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 ,[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],//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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],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 ,[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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 ,[object Object],[object Object],[object Object],[object Object]
THANK YOU

Más contenido relacionado

La actualidad más candente

Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
jeffz
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
Dmitri Nesteruk
 
Introduction of flex
Introduction of flexIntroduction of flex
Introduction of flex
vip_du
 

La actualidad más candente (19)

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

Destacado (12)

Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
 
C# linq
C# linqC# linq
C# linq
 
Hipaa
HipaaHipaa
Hipaa
 
Pharmacy
Pharmacy   Pharmacy
Pharmacy
 
Operating system
Operating systemOperating system
Operating system
 
Communique English Final Tal
Communique English Final TalCommunique English Final Tal
Communique English Final Tal
 
Transcutaneous bilirubin monitoring (bilichek)
Transcutaneous bilirubin monitoring (bilichek)Transcutaneous bilirubin monitoring (bilichek)
Transcutaneous bilirubin monitoring (bilichek)
 
Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01
 
Linq
LinqLinq
Linq
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Hand hygiene
Hand hygieneHand hygiene
Hand hygiene
 
LINQ
LINQLINQ
LINQ
 

Similar a PostThis

Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
Jevgeni Kabanov
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008
Luis Enrique
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
Kiev ALT.NET
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
Paulo Morgado
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
Woody Pewitt
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
g_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
Sachin Singh
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
Mody Farouk
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
Raga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
Satish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
singhadarsh
 

Similar a PostThis (20)

Linq intro
Linq introLinq intro
Linq intro
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
 
Visual studio 2008
Visual studio 2008Visual studio 2008
Visual studio 2008
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Language Integrated Query By Nyros Developer
Language Integrated Query By Nyros DeveloperLanguage Integrated Query By Nyros Developer
Language Integrated Query By Nyros Developer
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
Linq
LinqLinq
Linq
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Whats New In C# 3.0
Whats New In C# 3.0Whats New In C# 3.0
Whats New In C# 3.0
 
Whats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPontoWhats New In C# 4 0 - NetPonto
Whats New In C# 4 0 - NetPonto
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#Lo Mejor Del Pdc2008 El Futrode C#
Lo Mejor Del Pdc2008 El Futrode C#
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to CSharp
Introduction to CSharpIntroduction to CSharp
Introduction to CSharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 

PostThis

  • 2.
  • 3.
  • 4. Introduction RDBMS Business Logic Windows UI Web UI Console Web Service Windows Service Impedance Mismatch Data Access
  • 5.
  • 6.
  • 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.
  • 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.
  • 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.
  • 19.
  • 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.
  • 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.
  • 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.

Notas del editor

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.