Top Banner
Damien Guard (BSc, MBCS) http://damieng.com [email protected] Guernsey Software Developer Forum http://developers.org.gg Language Integrated Query: An introduction
22

Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Sep 20, 2018

Download

Documents

vutuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

DamienGuard(BSc,MBCS)http://[email protected]

GuernseySoftwareDeveloperForumhttp://developers.org.gg

Language Integrated Query: An introduction

Page 2: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

What is LINQ?

• LanguageIntegratedQuery

• Makequeryapartofthelanguage

• Componentof.NETFramework3.5

• NowshippingwithVisualStudio2008

Page 3: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Query without LINQ

• ObjectsusingloopsandcondiVonsforeach(Customer c in customers) if (c.Region == "UK") ...

• DatabasesusingSQLSELECT * FROM Customers WHERE Region='UK'

• XMLusingXPath/XQuery//Customers/Customer[@Region='UK']

Page 4: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

ADO without LINQSqlConnection con = new SqlConnection(...);con.Open(); SqlCommand cmd = new SqlCommand( @"SELECT * FROM Customers WHERE c.Region = @Region", con );cmd.Parameters.AddWithValue("@Region", "UK"); DataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string name = dr.GetString(dr.GetOrdinal("Name")); string phone = dr.GetString(dr.GetOrdinal("Phone")); DateTime date = dr.GetDateTime(3);}dr.Close();con.Close();

Page 5: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Query with LINQ

C#var myCustomers = from c in customers where c.Region == "UK" select c;

VB.NETDim myCustomers = From c In customers _ Where c.Region = "UK" _ Select c

Page 6: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

More LINQ queries

C#var goodCusts = (from c in db.Customers where c.PostCode.StartsWith("GY") orderby c.Sales descending select c).Skip(10).Take(10);

VB.NETDim goodCusts = (From c In db.Customers _ Where c.PostCode.StartsWith("GY") _ Order By c.Sales Descending _ Select c).Skip(1).Take(10)

Page 7: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Advantages

• UnifieddataaccessSinglesyntaxtolearnandremember

• StronglytypedCatcherrorsduringcompilaVon

• IntelliSensePromptforsyntaxanda]ributes

• Bindableresultsets

Page 8: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

ArchitectureOthersC# VB.NET

.NETLanguageIntegratedQuery(LINQ)

LINQtoSQL

LINQtoObjects

LINQtoXML

LINQtoDatasets

LINQtoEnVVes

LINQdatasourceproviders

ADO.NETsupportforLINQ

Page 9: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

LINQ to ObjectsC#int[] nums = new int[] {0,4,2,6,3,8,3,1};double average = nums.Take(6).Average();var above = from n in nums where n > average select n;

VB.NETDim nums() As Integer = {0,4,2,6,3,8,3,1}Double average = nums.Take(6).Average()Dim above = From n In nums _ Where n > average _ Select n

Page 10: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

LINQ to Objects

• QueryanyIEnumerable<T>sourceIncludesarrays,List<T>,DicVonary...

• ManyusefuloperatorsavailableSum,Max,Min,DisVnct,Intersect,Union

• ExposeyourowndatawithIEnumerable<T>orIQueryable<T>

• Createoperatorsusingextensionmethods

Page 11: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

LINQ operatorsAggregate Conversion Ordering ParVVoning Sets

AggregateAverageCountMaxMinSum

CastOfTypeToArrayToDictionaryToListToLookupToSequence

OrderByThenByDescendingReverse

SkipSkipWhileTakeTakeWhile

ConcatDistinctExceptIntersectUnion

andmanyothers

Page 12: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

LINQ to SQL• Object‐relaVonalmapping

Recordsbecomestrongly‐typedobjects

• Datacontextisthecontrollermechanism

• Facilitatesupdate,delete&insert

• TranslatesLINQqueriesbehindthescenes

• Type,parameterandinjecVonsafe

Page 13: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Database mapping

• VS2008designerorSQLMetalcommand

• Maptables&fieldstoclasses&properVes

• GeneratesparValclasseswitha]ributes

• Eachrecordbecomesanobject

• Datacontextrepresentsthedatabase

• UVlisetables,viewsorstoredprocedures

Page 14: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Modifying objects

• UpdateSetobjectproperVes

• Deletecontext.Table.DeleteOnSubmit(object)

• Insertcontext.Table.InsertOnSubmit(object)

• Commitchangesbackcontext.SubmitChanges()

TransacVonal‐allornothing

Page 15: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Demo of LINQ to SQL

Page 16: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Additional providers

• RelaVonaldataNHibernate,MySQL,Oracle,PostgreSQL

• WebservicesRDF,Flickr,Amazon,WebQueries

• CustomLDAP,GoogleDesktop,SharePoint,TerraServermaps

Page 17: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Future developments

• BlinqScaffoldwebUIforlist/view/updatepages

• PLINQParallelqueryprocessingovermanyCPUs

• SyncLINQ&ConVnuousLINQUpdatedresultsviaINoVfyCollecVonChanged

Page 18: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Limitations

LINQ• Onlydefinesquery,notupdateorcontext

LINQToSQL• MappingissetatcompileVme• Cannotmixmappedandunmapped

properVesinasinglequery• MicrosohSQLServer2000,2005only

Page 19: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

.NET features used

.NETFramework2.0

• ParValclasses(mapping)

.NETFramework3.5

• Anonymoustypes(shaping)

• Extensionmethods(queryoperators)

• Typeinference(varkeyword)

• Lambdaexpressions(querysyntax)

Page 20: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Alternatives for .NET

• NHibernate

• CastleMonoRail/AcVveRecord

• SubSonic

• CodegeneraVontool+templatesCodeSmith,MyGeneraVon,LLBLGen/Pro+NetTiers,DooDads,rollyourown...

Page 21: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

More information

• Officialsite‐msdn.microsoh.com/linq/

• Tutorials‐weblogs.asp.net/sco]gu/

• Screencasts‐Vnyurl.com/yusch

• ThispresentaVon&cheatsheetdamieng.com/blog/tag/linq

Page 22: Language Integrated Query: An introduction - … · Language Integrated Query: An introduction. What is LINQ? ... SQL LINQ to Objects LINQ to XML LINQ to Datasets LINQ to Enes LINQ

Questions & answers