Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Introduction  What is LINQ  Syntax  How to Query  Example Program.

Similar presentations


Presentation on theme: " Introduction  What is LINQ  Syntax  How to Query  Example Program."— Presentation transcript:

1

2  Introduction  What is LINQ  Syntax  How to Query  Example Program

3  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  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  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  Retrieves all integers from listInteger › listInteger is an array of integers › allInt will be an IEnumerable type var allInt = from l in listInteger select l; results of query stored in allInt from statement creates local instance of listInteger in l Select statement specifies what data to take

7  Statements between the from statement and select statement are used to refine results returned var greaterThanFive = from l in listInteger where l > 5 select l; Retrieves all integers from listInteger that are greater than 5

8  join  orderby … acsending  orderby … decending  group  count  min  Plus many more…

9  Using lambda expressions and functions: › var firstPosInt = allInts.First(a => a >= 0); › var posInt = (allInts.Where(a => a >= 0).Select(a => a);

10  3 steps in a LINQ query operation: › Obtain the data source › Create the query › Execute the query

11  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 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  DataContext will be generated from the DBML diagram  Create DataContext object in code › Name Format: “[NameOfDBMLFile]DataContext” MyDatabaseDataContext AWDatabse = new MyDatabaseDataContext(); //Put query here

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

15  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  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 int firstInt = allInt.First();

17  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  Microsoft Corporation. LINQ (Language-Integrated Query). Retrieved October 5, 2013 from http://msdn.microsoft.com/en-us/library/vstudio/bb397926.aspx 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  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)


Download ppt " Introduction  What is LINQ  Syntax  How to Query  Example Program."

Similar presentations


Ads by Google