1 / 21

LINQ

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

meara
Download Presentation

LINQ

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LINQ Joshua Clark Consultant SogetiUSA Email: joshua.clark@sogeti.com Blog: http://joshua-clark.blogspot.com

  2. About Me - C#/ASP.NET - SQL Server 2005 Reporting Services - SQL Server 2005 - LINQ to SQL Development- Web Services

  3. What is LINQ? • Quick LINQ Basics Overview • LINQ to Objects • LINQ to SQL • Q & A Agenda

  4. What is LINQ? • Language Integrated Query • It’s a technology that bridges the gap between programming languages and data

  5. <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

  6. 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

  7. 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); • }

  8. 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

  9. LINQ to Objects demo

  10. LINQ to SQL – The Challenge Data != Objects

  11. 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

  12. 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

  13. 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

  14. LINQ to SQL demo

  15. 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()

  16. 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) ) );

  17. LINQ to XML demo

  18. 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

  19. Linqpad.net demo

  20. 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

  21. “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

More Related