Introduction What is LINQ Syntax How to Query Example Program
Language INtegrated Query Released as a part of the.Net Framework 3.5 › November 19, 2007 Supported in the.Net languages › C#, VB.Net
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
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
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
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
join orderby … acsending orderby … decending group count min Plus many more…
Using lambda expressions and functions: › var firstPosInt = allInts.First(a => a >= 0); › var posInt = (allInts.Where(a => a >= 0).Select(a => a);
3 steps in a LINQ query operation: › Obtain the data source › Create the query › Execute the query
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
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
DataContext will be generated from the DBML diagram Create DataContext object in code › Name Format: “[NameOfDBMLFile]DataContext” MyDatabaseDataContext AWDatabse = new MyDatabaseDataContext(); //Put query here
Create new XDocument object from XML File: XDocument xml =
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;
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();
Universal syntax when querying in memory data, SQL Servers, or XML files Syntax resembles SQL Features make coding quicker and less prone to errors
Microsoft Corporation. LINQ (Language-Integrated Query). Retrieved October 5, 2013 from Ferracchiati, F. LINQ for Visual C# New York, NY: Spinger-Verlag New York Inc, Russo, Marco, and Paolo Pialorsi. Introducing Microsoft LINQ. Redmond, WA: Microsoft, Guthrie, Scott. "Using LINQ to SQL (part 1)." ScottGu's Blog. Microsoft, 19 May Web. 5 Oct AdventureWorks2008R2 Database and ObjectDumper Class courtesy of Microsoft › AdventureWorks2008R2 available at › ObjectDumper available in this package at
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)