Upload
carmel-blair
View
217
Download
0
Embed Size (px)
Citation preview
Microsoft LINQRyan 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?
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
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
Syntax Retrieves all integers from listInteger
› listInteger is an array of integers› allInt will be an IEnumerable<int> 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
Syntax: Filtering
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
Syntax: Other Common Operations
join orderby … acsending orderby … decending group count min Plus many more…
Syntax: Alternate Query Forms
Using lambda expressions and functions:› var firstPosInt = allInts.First(a => a >= 0);
› var posInt = (allInts.Where(a => a >= 0).Select(a => a);
How to Query with LINQ
3 steps in a LINQ query operation:› Obtain the data source› Create the query › Execute the query
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
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
SQL Server Database: DataContext
DataContext will be generated from the DBML diagram
Create DataContext object in code› Name Format:
“[NameOfDBMLFile]DataContext”
MyDatabaseDataContext AWDatabse = new MyDatabaseDataContext();
//Put query here
XML Documents: Create new XDocument object from
XML File:
XDocument xml = XDocument.Load(@”C:\Users\Example\
myXmlDoc.xml”);
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;
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
allIntint firstInt = allInt.First();
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
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
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)
LINQExample.zip