260 likes | 267 Views
Explore the history of LINQ and its role in data mapping with LINQ to SQL. Learn about query execution, LINQ parts, and key elements like extension methods, lambda expressions, and more. Discover LINQ query syntax and practical examples. Dive into LINQ to XML, LINQ data access, and the Entity Framework. Understand the differences between LINQ and ADO.Net data access. Get insights into LINQ to SQL architecture and services. Follow valuable resources to start with LINQ effectively.
E N D
ADO vNext LINQ LINQ to SQL Entity Framework FreekLeemhuisfreek.leemhuis@logica.com
Microsoft en ORM – de historie Microsoft en ORM – de historie WinFS
Defining LINQ and LINQ to SQL • Bestaat uit taal uitbreidingen in C# 3.0 en VB 9.0 • Uniforme manier om queries uit te voeren op objecten LINQ LINQ to SQL • Een framework (OR mapper) voor het mappen van data classes op SQL Server tables, views en stored procedures.
LINQ parts .Net APIs Providers Interne query engine LINQ to Datasets LINQ to SQL LINQ to XML LINQ to Entities LINQ to Objects IEnumerable IQueryable C# 3.0 VB 9.0 Others LINQ
Taaluitbreidingen • Extension methods • Partial Methods • Lambda expressions • Anonymous types • Object initializers • Local variable inference • Linq keywords and operators
Demo • Extention methods • Local variable inference • Anonymous types Demo
De basis van LINQ • LINQ queries gaan over elementen uit sequences • Een sequence is een object dat de IEnumerable<T> interface implementeert Bijvoorbeeld: String[] namen = {“Maarten”, “Ralph”, “Freek”}; Selectie = namen.Where(n=>n.Contains(“a”)) .Orderby(n=>n) .Select(n=>n.ToUpper());
Comprehension Syntax Demo
Query voorbeelden Query composition var filtered = names .Where (n => n.Contains ("a")); var sorted = filtered.OrderBy (n => n); var query = sorted.Select (n => n.ToUpper( ));
LINQ to XML • Demo
Data Access met LINQ LINQ to SQL
Object Relational Impedance Mismatch Objects != Data Relationele Data Objecten
Data Access met ADO.Net Data Access met klassiekADO.Net Queries in quotes SqlConnection c = new SqlConnection(…); c.Open(); SqlCommandcmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0"); cmd.Parameters.AddWithValue("@p0", "London“); DataReaderdr = c.Execute(cmd); while (dr.Read()) { string name = dr.GetString(0); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); } dr.Close(); Loosely bound arguments Loosely typed result sets No compile time checks
Data Access met LINQ to SQL Classes describe data public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … } Tables are like collections Strongly typed connection Northwind db = new Northwind(…); var contacts = from c in db.Customers where c.City == "London" select new { c.Name, c.Phone }; Integrated query syntax Strongly typed results
Linq to SQL : Mapping DB to Objects DataContext Database Table View Column Relationship Class + Collection Class + Collection Property Nested Collection Method Stored Procedure
LINQ to SQLDataContext LINQ to SQL Architectuur from c in Context.Customers Where c.LastName.StartsWith(“Niks”) select new { c.Name, c.FirstName}; Customer c = new Customer();Customer.LastName = “Bos”;Customers.InsertOnSumbit(c);Context.SubmitChanges(); Services:- Change tracking- Concurrency control- Object identity select Name, FirstNamefrom customerswhere Lastname like ‘Niks%' Dynamische SQLof Stored Procedure Applicatie SQL Server
LINQ to SQL Demo Title of Presentation
Het ADO.Net Entity Framework Conceptual Mapping Logical CSDL MSL SSDL Object Model Relational Data Table Entity Table Entity Table Entity Table Entity Table
Entity Relationship Model Dr. Peter Chen, 1970
Entity Framework Architecture Object Services LINQ to Entities Object Query Entity SQL Entity SQL Entity Client Entity Framework Layers Conceptual CSDL Mapping MSL Logical SSDL
Resources Starten met Linq http://msdn2.microsoft.com/en-us/library/bb308961.aspx http://www.asp.net/learn/linq-videos/ http://weblogs.asp.net/scottgu/archive/tags/LINQ/ http://www.hookedonlinq.com/ http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx Linqninjas http://blogs.msdn.com/mattwar/default.aspx http://weblogs.asp.net/fbouma http://codebetter.com/blogs/ian_cooper/ http://mtaulty.com LINQPad http://www.linqpad.net/