1 / 22

Language Integrated Query: An introduction

Learn about LINQ, a query language and component of .NET Framework 3.5 that allows you to query data from various sources such as objects, databases, and XML. Understand its advantages, architecture, and how to use LINQ with C# and VB.NET. Explore LINQ to Objects, LINQ to SQL, and LINQ to XML. Get insights into LINQ data source providers, LINQ operators, and LINQ to SQL features like object-relational mapping and modifying objects. Discover additional providers, limitations, alternatives, and resources for learning more about LINQ.

susanpate
Download Presentation

Language Integrated Query: An introduction

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. Language Integrated Query: An introduction Damien Guard (BSc, MBCS) http://damieng.com damien@envytech.co.uk Guernsey Software Developer Forum http://developers.org.gg

  2. What is LINQ? • Language Integrated Query • Make query a part of the language • Component of .NET Framework 3.5 • Now shipping with Visual Studio 2008

  3. Query without LINQ • Objects using loops and conditionsforeach(Customer c in customers) if (c.Region == "UK") ... • Databases using SQLSELECT * FROM Customers WHERE Region='UK' • XML using XPath/XQuery//Customers/Customer[@Region='UK']

  4. ADO without LINQ SqlConnection con = new SqlConnection(...); con.Open(); SqlCommand cmd = new SqlCommand( @"SELECT * FROM Customers WHERE c.Region = @Region", con ); cmd.Parameters.AddWithValue("@Region", "UK"); DataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string name = dr.GetString(dr.GetOrdinal("Name")); string phone = dr.GetString(dr.GetOrdinal("Phone")); DateTime date = dr.GetDateTime(3); } dr.Close(); con.Close();

  5. Query with LINQ C#var myCustomers = from c in customers where c.Region == "UK" select c; VB.NETDim myCustomers = From c In customers _ Where c.Region = "UK" _ Select c

  6. More LINQ queries C#var goodCusts = (from c in db.Customers where c.PostCode.StartsWith("GY") orderby c.Sales descending select c).Skip(10).Take(10); VB.NETDim goodCusts = (From c In db.Customers _ Where c.PostCode.StartsWith("GY") _ Order By c.Sales Descending _ Select c).Skip(1).Take(10)

  7. Advantages • Unified data accessSingle syntax to learn and remember • Strongly typedCatch errors during compilation • IntelliSensePrompt for syntax and attributes • Bindable result sets

  8. Architecture C# VB.NET Others .NET Language Integrated Query (LINQ) LINQ data source providers ADO.NET support for LINQ LINQto Objects LINQto Datasets LINQto SQL LINQto Entities LINQto XML

  9. LINQ to Objects C#int[] nums = new int[] {0,4,2,6,3,8,3,1};double average = nums.Take(6).Average();var above = from n in nums where n > average select n; VB.NETDim nums() As Integer = {0,4,2,6,3,8,3,1}Double average = nums.Take(6).Average()Dim above = From n In nums _ Where n > average _ Select n

  10. LINQ to Objects • Query any IEnumerable<T> sourceIncludes arrays, List<T>, Dictionary... • Many useful operators availableSum, Max, Min, Distinct, Intersect, Union • Expose your own data with IEnumerable<T> or IQueryable<T> • Create operators using extension methods

  11. LINQ operators and many others

  12. LINQ to SQL • Object-relational mappingRecords become strongly-typed objects • Data context is the controller mechanism • Facilitates update, delete & insert • Translates LINQ queries behind the scenes • Type, parameter and injection safe

  13. Database mapping • VS 2008 designer or SQLMetal command • Map tables & fields to classes & properties • Generates partial classes with attributes • Each record becomes an object • Data context represents the database • Utilise tables, views or stored procedures

  14. Modifying objects • UpdateSet object properties • Deletecontext.Table.DeleteOnSubmit(object) • Insertcontext.Table.InsertOnSubmit(object) • Commit changes backcontext.SubmitChanges()Transactional - all or nothing

  15. Demo of LINQ to SQL

  16. Additional providers • Relational dataNHibernate, MySQL, Oracle, PostgreSQL • Web servicesRDF, Flickr, Amazon, WebQueries • CustomLDAP, Google Desktop, SharePoint, TerraServer maps

  17. Future developments • BlinqScaffold web UI for list/view/update pages • PLINQParallel query processing over many CPUs • SyncLINQ & Continuous LINQUpdated results via INotifyCollectionChanged

  18. Limitations LINQ • Only defines query, not update or context LINQ To SQL • Mapping is set at compile time • Can not mix mapped and unmapped properties in a single query • Microsoft SQL Server 2000, 2005 only

  19. .NET features used .NET Framework 2.0 • Partial classes (mapping) .NET Framework 3.5 • Anonymous types (shaping) • Extension methods (query operators) • Type inference (var keyword) • Lambda expressions (query syntax)

  20. Alternatives for .NET • NHibernate • Castle MonoRail / ActiveRecord • SubSonic • Code generation tool + templatesCodeSmith, MyGeneration, LLBLGen/Pro +NetTiers, DooDads, roll your own...

  21. More information • Official site - msdn.microsoft.com/linq/ • Tutorials - weblogs.asp.net/scottgu/ • Screencasts - tinyurl.com/yuscft • This presentation & cheat sheetdamieng.com/blog/tag/linq

  22. Questions & answers

More Related