19
Microsoft LINQ Ryan Wiederholt

Introduction What is LINQ Syntax How to Query Example Program

Embed Size (px)

Citation preview

Page 1: Introduction  What is LINQ  Syntax  How to Query  Example Program

Microsoft LINQRyan Wiederholt

Page 2: Introduction  What is LINQ  Syntax  How to Query  Example Program

Agenda

Introduction What is LINQ Syntax How to Query Example Program

Page 3: 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

Page 4: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 5: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 6: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 7: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 8: Introduction  What is LINQ  Syntax  How to Query  Example Program

Syntax: Other Common Operations

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

Page 9: Introduction  What is LINQ  Syntax  How to Query  Example Program

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);

Page 10: Introduction  What is LINQ  Syntax  How to Query  Example Program

How to Query with LINQ

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

Page 11: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 12: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 13: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 14: Introduction  What is LINQ  Syntax  How to Query  Example Program

XML Documents: Create new XDocument object from

XML File:

XDocument xml = XDocument.Load(@”C:\Users\Example\

myXmlDoc.xml”);

Page 15: Introduction  What is LINQ  Syntax  How to Query  Example Program

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;

Page 16: Introduction  What is LINQ  Syntax  How to Query  Example Program

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();

Page 17: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 18: Introduction  What is LINQ  Syntax  How to Query  Example Program

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

Page 19: Introduction  What is LINQ  Syntax  How to Query  Example Program

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