210 likes | 408 Views
LINQ. Joshua Clark Consultant Sogeti USA Email: joshua.clark@sogeti.com Blog: http://joshua-clark.blogspot.com. About Me - C#/ASP.NET - SQL Server 2005 Reporting Services - SQL Server 2005 - LINQ to SQL Development - Web Services. What is LINQ? Quick LINQ Basics Overview
E N D
LINQ Joshua Clark Consultant SogetiUSA Email: joshua.clark@sogeti.com Blog: http://joshua-clark.blogspot.com
About Me - C#/ASP.NET - SQL Server 2005 Reporting Services - SQL Server 2005 - LINQ to SQL Development- Web Services
What is LINQ? • Quick LINQ Basics Overview • LINQ to Objects • LINQ to SQL • Q & A Agenda
What is LINQ? • Language Integrated Query • It’s a technology that bridges the gap between programming languages and data
<book> <title/> <author/> <year/> <price/> </book> Relational Objects XML The LINQ Project .NET Language Integrated Query C# 3.0 Visual Basic 9.0 Others LINQ toObjects LINQ toDataSets LINQ toSQL LINQ toEntities LINQ toXML
What you need to LINQ • Visual C# 2008 Express Edition • Visual Basic 2008 Express Edition • Visual Web Developer 2008 Express • Visual Studio 2008 Std. or higher • LINQ to SQL • SQL Server 2005 Express • SQL Server 2000 • SQL Server 2005 • SQL Server 2008* - New data types not supported by first release of LINQ to SQL
LINQ to Objects • string[]mystuff = • {“linq”, “is”, “awesome”}; • varwordswithl = from stuff in mystuff • wherestuff.Contains(“l”) • select stuff; • foreach(var n in wordswithl) • { • Console.WriteLine(n); • }
C# 3.0/VB 9 Language Enhancements • Implicitly Typed Variables • varmystring = “MyString” • Object Initializers • Customer c = new Customer() { Name = “Joe”, Address = “123 West St.”}; • Lambda Expressions • (c => c.CompanyName == “Sogeti”) • delegate( Customer c) {return c.CompanyName == “Sogeti”} • Extension Methods • .Select(), .Where(), .Single(), .FirstOrDefault() • Anonymous Types • Types that are automatically generated for us by the compiler
LINQ to Objects demo
LINQ to SQL – The Challenge Data != Objects
LINQ to SQL • A new data access API that we can use to work with SQL Server • An object relational mapper that takes us from objects in memory to the database and back again • Several tools that we can use to help bridge the gap between our object model and our relational model • LINQ Pattern/Syntax put on top this new API
LINQ to SQL • Design Points • Rapid application development against SQL Server Database • Direct mapping to Microsoft SQL Server schema • Mappings expressed in attributes in XML file • 1-to-1 .NET Classes and SQL tables • Can bring in Stored Procedures, Table-valued functions , Views • Foreign keys are shown as relationships – designer builds the relationship for us automatically from database • Customization • Business Logic • Partial Classes for generated Objects – does not get wiped out when you re-generate your auto-generate code • Update Logic • Call SP to invoke custom logic
LINQ to SQL Typical ADO.NET data-access code today… using (SqlConnectionconn = new SqlConnection(“…”)) { conn.Open(); SqlCommandcmd = conn.CreateCommand(); cmd = CommandText = “SELECT Name, Country FROM Customers WHERE City = @City”; cmd.Parameters.AddWithValue(“@City”, “Cleveland”); using(SqlDataReaderrdr = cmd.ExecuteReader()) { while(rdr.Read()) { string name = rdr.GetString(0); string country = rdr.GetString(1); …. } } } SQL Query is a string Loosely bound parameters Loosely type columns
LINQ to SQL demo
Understanding Deferred Query Execution • Without lazy loading LINQ queries would perform poorly • Important thing to understand about LINQ queries is not their result is but what they can return • Allows us to create a query and reuse it at a later time • Forcing Immediate Execution… • In some instances, we may need to get all the data from a sequence and we do not want to iterate over every item • query.ToList(), query.ToArray()
Must reference System.Xml.Linq • Constructing XML in a very straight-forward approach • Constructors help with creating XML LINQ to XML XElement contacts= new XElement(“contacts”, from c in customers where c.Country == “USA” select new XElement(“contact”, new Xelement(“name”, c.CompanyName), new Xelement(“phone” , c.Phone) ) );
LINQ to XML demo
Pros • FREE tool to use to run LINQ queries • Expedites the process of shaping your data • Can connect to any database • Cons • No intellisense on LINQ syntax • Object names are case-sensitive LINQPad.NET
Linqpad.net demo
Data != Objects • Imperative => Declarative • Query against objects, databases, XML • Get LinqPad.net • More Information on LINQ • - http://msdn.microsoft.com/data/ref/linq • - http://linqinaction.net – Get the book! • - http://miketaulty.com/blog • - http://danielmoth.com/blog Summary
“LINQ in Action”, FabriceMargurie, Steve Eichert, Jim Wooley • MSDN Webcasts series • Daniel Moth, Microsoft , LINQ screencasts • MikeTaulty, Microsoft , LINQ to SQL screencasts • Marc Schweigert, Microsoft , webcast References