Language Integrated Query: An introduction - ?· Language Integrated Query: An introduction. What is…

  • Published on
    20-Sep-2018

  • View
    212

  • Download
    0

Embed Size (px)

Transcript

  • DamienGuard(BSc,MBCS)http://damieng.comdamien@envytech.co.uk

    GuernseySoftwareDeveloperForumhttp://developers.org.gg

    LanguageIntegratedQuery:Anintroduction

    http://damieng.comhttp://damieng.commailto:damien@envytech.co.ukmailto:damien@envytech.co.ukhttp://www.developer.org.gghttp://www.developer.org.gg

  • WhatisLINQ?

    LanguageIntegratedQuery

    Makequeryapartofthelanguage

    Componentof.NETFramework3.5

    NowshippingwithVisualStudio2008

  • QuerywithoutLINQ

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

    DatabasesusingSQLSELECT * FROM Customers WHERE Region='UK'

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

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

  • QuerywithLINQ

    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

  • MoreLINQqueries

    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)

  • Advantages

    UnifieddataaccessSinglesyntaxtolearnandremember

    StronglytypedCatcherrorsduringcompilaVon

    IntelliSensePromptforsyntaxanda]ributes

    Bindableresultsets

  • ArchitectureOthersC# VB.NET

    .NETLanguageIntegratedQuery(LINQ)

    LINQtoSQL

    LINQtoObjects

    LINQtoXML

    LINQtoDatasets

    LINQtoEnVVes

    LINQdatasourceproviders

    ADO.NETsupportforLINQ

  • LINQtoObjectsC#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

  • LINQtoObjects

    QueryanyIEnumerablesourceIncludesarrays,List,DicVonary...

    ManyusefuloperatorsavailableSum,Max,Min,DisVnct,Intersect,Union

    ExposeyourowndatawithIEnumerableorIQueryable

    Createoperatorsusingextensionmethods

  • LINQoperatorsAggregate Conversion Ordering ParVVoning Sets

    AggregateAverageCountMaxMinSum

    CastOfTypeToArrayToDictionaryToListToLookupToSequence

    OrderByThenByDescendingReverse

    SkipSkipWhileTakeTakeWhile

    ConcatDistinctExceptIntersectUnion

    andmanyothers

  • LINQtoSQL ObjectrelaVonalmapping

    Recordsbecomestronglytypedobjects

    Datacontextisthecontrollermechanism

    Facilitatesupdate,delete&insert

    TranslatesLINQqueriesbehindthescenes

    Type,parameterandinjecVonsafe

  • Databasemapping

    VS2008designerorSQLMetalcommand

    Maptables&fieldstoclasses&properVes

    GeneratesparValclasseswitha]ributes

    Eachrecordbecomesanobject

    Datacontextrepresentsthedatabase

    UVlisetables,viewsorstoredprocedures

  • Modifyingobjects

    UpdateSetobjectproperVes

    Deletecontext.Table.DeleteOnSubmit(object)

    Insertcontext.Table.InsertOnSubmit(object)

    Commitchangesbackcontext.SubmitChanges()

    TransacVonalallornothing

  • DemoofLINQtoSQL

  • Additionalproviders

    RelaVonaldataNHibernate,MySQL,Oracle,PostgreSQL

    WebservicesRDF,Flickr,Amazon,WebQueries

    CustomLDAP,GoogleDesktop,SharePoint,TerraServermaps

  • Futuredevelopments

    BlinqScaffoldwebUIforlist/view/updatepages

    PLINQParallelqueryprocessingovermanyCPUs

    SyncLINQ&ConVnuousLINQUpdatedresultsviaINoVfyCollecVonChanged

  • Limitations

    LINQ Onlydefinesquery,notupdateorcontext

    LINQToSQL MappingissetatcompileVme Cannotmixmappedandunmapped

    properVesinasinglequery MicrosohSQLServer2000,2005only

  • .NETfeaturesused

    .NETFramework2.0

    ParValclasses(mapping).NETFramework3.5

    Anonymoustypes(shaping) Extensionmethods(queryoperators) Typeinference(varkeyword) Lambdaexpressions(querysyntax)

  • Alternativesfor.NET

    NHibernate

    CastleMonoRail/AcVveRecord

    SubSonic

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

  • Moreinformation

    Officialsitemsdn.microsoh.com/linq/

    Tutorialsweblogs.asp.net/sco]gu/

    ScreencastsVnyurl.com/yusch

    ThispresentaVon&cheatsheetdamieng.com/blog/tag/linq

    http://msdn.microsoft.com/linqhttp://msdn.microsoft.com/linqhttp://weblogs.asp.net/scottgu/http://weblogs.asp.net/scottgu/http://tinyurl.com/yuscfthttp://tinyurl.com/yuscfthttp://damieng.com/blog/tag/linqhttp://damieng.com/blog/tag/linq

  • Questions&answers

Recommended

View more >