1 / 19

Microsoft LINQ

Microsoft LINQ. Ryan Wiederholt. Agenda. Introduction What is LINQ Syntax How to Query Example Program. Introduction. Language INtegrated Query Released as a part of the .Net Framework 3.5 November 19, 2007 Supported in the .Net languages C#, VB.Net. What is LINQ?.

lilith
Download Presentation

Microsoft 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. Microsoft LINQ Ryan Wiederholt

  2. Agenda • Introduction • What is LINQ • Syntax • How to Query • Example Program

  3. Introduction • Language INtegrated Query • Released as a part of the .Net Framework 3.5 • November 19, 2007 • Supported in the .Net languages • C#, VB.Net

  4. What is LINQ? • Used as a universal query language for retrieving data from containers • Appropriate data containers include: • Arrays • .Net containers such as List and Dictionary • Any IEnumerable collection • SQL Server Database • XML Document

  5. Syntax • Resembles SQL in reverse • Introduces new variable type “var” • Compiler determines the type • Still considered strongly typed • Creates “anonymous type” if data type cannot be determined

  6. Syntax • Retrieves all integers from listInteger • listInteger is an array of integers • allInt will be an IEnumerable<int> type from statement creates local instance of listInteger in l varallInt = from l in listInteger select l; results of query stored in allInt Select statement specifies what data to take

  7. Syntax: Filtering • Retrieves all integers from listInteger that are greater than 5 • Statements between the from statement and select statement are used to refine results returned vargreaterThanFive = from l in listInteger where l > 5 select l;

  8. Syntax: Other Common Operations • join • orderby … acsending • orderby … decending • group • count • min • Plus many more…

  9. Syntax: Alternate Query Forms • Using lambda expressions and functions: • varfirstPosInt = allInts.First(a => a >= 0); • varposInt = (allInts.Where(a => a >= 0).Select(a => a);

  10. How to Query with LINQ • 3 steps in a LINQ query operation: • Obtain the data source • Create the query • Execute the query

  11. Obtain the Data Source • Data Sources can be in memory containers, SQL Server databases or XML documents • In memory data sources need no additional preparation after they have been created • SQL Server Databases must be set up using the DBML Designer in Visual Studio • XML documents must be loaded into an XDocument object

  12. SQL Server Database: DBML Designer • Add new LINQ to SQL Class to project • Connect to Database using Server Explorer • Drag tables over to designer pane • Relationships will be set up automatically

  13. SQL Server Database: DataContext • DataContext will be generated from the DBML diagram • Create DataContext object in code • Name Format: “[NameOfDBMLFile]DataContext” MyDatabaseDataContextAWDatabse = new MyDatabaseDataContext(); //Put query here

  14. XML Documents: • Create new XDocument object from XML File: XDocument xml = XDocument.Load(@”C:\Users\Example\myXmlDoc.xml”);

  15. Next Step: Create the Query • Syntax is constant for all types of data sources • When querying XML files • Use Elements() and Element() methods to specify tags to query • From x in xmlObject.Elements(“TopTag”) order by (int)x.Element(“id”) select x;

  16. Final Step: Execute the Query • Query is not executed until the variable is used in code • foreach loop is a common way of accessing query results • Use methods to grab specific elements • Takes first item returned from the query allInt • intfirstInt = allInt.First();

  17. Review • Universal syntax when querying in memory data, SQL Servers, or XML files • Syntax resembles SQL • Features make coding quicker and less prone to errors

  18. References • Microsoft Corporation. LINQ (Language-Integrated Query). Retrieved October 5, 2013 from http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx • Ferracchiati, F. LINQ for Visual C# 2008. New York, NY: Spinger-Verlag New York Inc, 2008. • Russo, Marco, and Paolo Pialorsi. Introducing Microsoft LINQ. Redmond, WA: Microsoft, 2007.   • Guthrie, Scott. "Using LINQ to SQL (part 1)." ScottGu's Blog. Microsoft, 19 May 2007. Web. 5 Oct. 2013. • AdventureWorks2008R2 Database and ObjectDumper Class courtesy of Microsoft • AdventureWorks2008R2 available at http://msftdbprodsamples.codeplex.com/releases/view/93587 • ObjectDumper available in this package at http://code.msdn.microsoft.com/csharpsamples/Release/ProjectReleases.aspx?ReleaseId=8

  19. Sample Code • Entire project is in compressed file. • Some code is commented out due to needing SQL Express installed and the proper database (which is too large to embed here)

More Related