LINQ Providers

of 17 /17
LINQ Providers LINQ Providers Or why .NET rules, and Java Or why .NET rules, and Java doesn’t doesn’t Branimir Branimir Giurov Giurov SofiaDev.org UG Lead, C# MVP SofiaDev.org UG Lead, C# MVP Freelance Software Developer Freelance Software Developer www. sofiadev.org

description

LINQ Providers. Or why .NET rules, and Java doesn’t. Branimir Giurov. SofiaDev.org UG Lead, C# MVP Freelance Software Developer. www. sofiadev.org. Contents. typeof ( linq ) How does it work? Lambda, extension methods, iterators. typeof ( linq ). - PowerPoint PPT Presentation

Transcript of LINQ Providers

Page 1: LINQ Providers

LINQ ProvidersLINQ ProvidersLINQ ProvidersLINQ ProvidersOr why .NET rules, and Java doesn’t Or why .NET rules, and Java doesn’t

Branimir GiurovBranimir GiurovSofiaDev.org UG Lead, C# MVPSofiaDev.org UG Lead, C# MVP

Freelance Software DeveloperFreelance Software Developer

www.sofiadev.org

Page 2: LINQ Providers

ContentsContentsContentsContents

• typeof(linq)typeof(linq)

• How does it work?How does it work?

• Lambda, extension methods, iteratorsLambda, extension methods, iterators

• typeof(linq)typeof(linq)

• How does it work?How does it work?

• Lambda, extension methods, iteratorsLambda, extension methods, iterators

Page 3: LINQ Providers

typeof(linq)typeof(linq)typeof(linq)typeof(linq)

Page 4: LINQ Providers

ObjectsObjects

<book><book> <title/><title/> <author/><author/> <year/><year/> <price/><price/></book></book>

XMLXML

.NET Language Integrated Query.NET Language Integrated Query

C# 3.0C# 3.0 VB 9.0VB 9.0 Others…Others…

RelationalRelational

LINQ toLINQ toObjectsObjects

LINQ toLINQ toSQLSQL

LINQ toLINQ toXMLXML

LINQ toLINQ toEntitiesEntities

LINQ toLINQ toDatasetsDatasets

Page 5: LINQ Providers

typeof(linq)typeof(linq)typeof(linq)typeof(linq)

• LINQ to ObjectsLINQ to Objects

• LINQ to SQLLINQ to SQL

• LINQ to XMLLINQ to XML

• LINQ to DatasetsLINQ to Datasets

• LINQ to EntitiesLINQ to Entities

• LINQ to Windows SearchLINQ to Windows Search

• LINQ to GoogleLINQ to Google

• LINQ to SharepointLINQ to Sharepoint

• LINQ to nHibernateLINQ to nHibernate

• LINQ to ObjectsLINQ to Objects

• LINQ to SQLLINQ to SQL

• LINQ to XMLLINQ to XML

• LINQ to DatasetsLINQ to Datasets

• LINQ to EntitiesLINQ to Entities

• LINQ to Windows SearchLINQ to Windows Search

• LINQ to GoogleLINQ to Google

• LINQ to SharepointLINQ to Sharepoint

• LINQ to nHibernateLINQ to nHibernate

Page 6: LINQ Providers

LINQ to ObjectsLINQ to ObjectsLINQ to ObjectsLINQ to Objects

• Query language over collections of Query language over collections of objectsobjects

• Standard SQL-like set of query Standard SQL-like set of query operators, including joins and groupingoperators, including joins and grouping

• New query operators can be added, fully New query operators can be added, fully extensible (extension methods)extensible (extension methods)

• Intellisense and compiler syntax Intellisense and compiler syntax checking support in Visual Studiochecking support in Visual Studio

• Query language over collections of Query language over collections of objectsobjects

• Standard SQL-like set of query Standard SQL-like set of query operators, including joins and groupingoperators, including joins and grouping

• New query operators can be added, fully New query operators can be added, fully extensible (extension methods)extensible (extension methods)

• Intellisense and compiler syntax Intellisense and compiler syntax checking support in Visual Studiochecking support in Visual Studio

Page 7: LINQ Providers

LINQ to ObjectsLINQ to ObjectsLINQ to ObjectsLINQ to Objects

• How to query an array of int?How to query an array of int?• How to query an array of int?How to query an array of int?

public void Linq1() {public void Linq1() {    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var lowNums =    var lowNums =        from n in numbers        from n in numbers        where n < 5        where n < 5        select n;        select n;

    Console.WriteLine("Numbers < 5:");    Console.WriteLine("Numbers < 5:");    foreach (var x in lowNums) {    foreach (var x in lowNums) {        Console.WriteLine(x);        Console.WriteLine(x);    }    }}}

Page 8: LINQ Providers

LINQ to SQLLINQ to SQLLINQ to SQLLINQ to SQL

• Write queries in any .NET language to retrieve and Write queries in any .NET language to retrieve and manipulate data from SQLmanipulate data from SQL

• Use FK constraints for defining relations of data Use FK constraints for defining relations of data without joinswithout joins

• Join in code on any column that has no FK definedJoin in code on any column that has no FK defined

• Use FK’s to query/modify data in SQL DBUse FK’s to query/modify data in SQL DB

• Call Call SubmitChanges()SubmitChanges() when you’re done when you’re done

• Currently supports SQL 2000 & 2005Currently supports SQL 2000 & 2005• System.Date.Linq.SqlClient.Sql2000ProviderSystem.Date.Linq.SqlClient.Sql2000Provider

• System.Date.Linq.SqlClient.Sql2005ProviderSystem.Date.Linq.SqlClient.Sql2005Provider

• Write queries in any .NET language to retrieve and Write queries in any .NET language to retrieve and manipulate data from SQLmanipulate data from SQL

• Use FK constraints for defining relations of data Use FK constraints for defining relations of data without joinswithout joins

• Join in code on any column that has no FK definedJoin in code on any column that has no FK defined

• Use FK’s to query/modify data in SQL DBUse FK’s to query/modify data in SQL DB

• Call Call SubmitChanges()SubmitChanges() when you’re done when you’re done

• Currently supports SQL 2000 & 2005Currently supports SQL 2000 & 2005• System.Date.Linq.SqlClient.Sql2000ProviderSystem.Date.Linq.SqlClient.Sql2000Provider

• System.Date.Linq.SqlClient.Sql2005ProviderSystem.Date.Linq.SqlClient.Sql2005Provider

Page 9: LINQ Providers

DEMODEMODEMODEMOLINQ to SQLLINQ to SQL

ToString()ToString()Modifying QueriesModifying Queries

Mapping StylesMapping StylesMapping ToolsMapping Tools

Page 10: LINQ Providers

LINQ to XMLLINQ to XMLLINQ to XMLLINQ to XML

• LINQ to XML is a new way to construct, write and read LINQ to XML is a new way to construct, write and read XML data in the .NET language of the developers’ XML data in the .NET language of the developers’ choicechoice

• Simplifies working with XML Simplifies working with XML • No more XPath or XSLTNo more XPath or XSLT

• Not a replacement for DOM or any of the current XML libsNot a replacement for DOM or any of the current XML libs

• Supports writing Query Expressions Supports writing Query Expressions

• Can be combined with any of the other LINQ technologiesCan be combined with any of the other LINQ technologies

• Supports SAX-style reading/writing of documents with the use Supports SAX-style reading/writing of documents with the use of XStreamingElementof XStreamingElement• Differed executionDiffered execution

• LINQ to XML is a new way to construct, write and read LINQ to XML is a new way to construct, write and read XML data in the .NET language of the developers’ XML data in the .NET language of the developers’ choicechoice

• Simplifies working with XML Simplifies working with XML • No more XPath or XSLTNo more XPath or XSLT

• Not a replacement for DOM or any of the current XML libsNot a replacement for DOM or any of the current XML libs

• Supports writing Query Expressions Supports writing Query Expressions

• Can be combined with any of the other LINQ technologiesCan be combined with any of the other LINQ technologies

• Supports SAX-style reading/writing of documents with the use Supports SAX-style reading/writing of documents with the use of XStreamingElementof XStreamingElement• Differed executionDiffered execution

Page 11: LINQ Providers

LINQ to XMLLINQ to XMLLINQ to XMLLINQ to XML

• Combining it with other LINQ technologies makes life Combining it with other LINQ technologies makes life easiereasier

• Combining it with other LINQ technologies makes life Combining it with other LINQ technologies makes life easiereasierXElement xml = XElement xml = new XElement("contacts", XElement("contacts",

new XElement("contact", XElement("contact", new XAttribute("contactId", "2"), XAttribute("contactId", "2"),

new XElement("firstName", "Barry"), XElement("firstName", "Barry"),new XElement("lastName", "Gottshall") ), new XElement("lastName", "Gottshall") ),

new XElement("contact",new XElement("contact", new XAttribute("contactId", "3"),new XAttribute("contactId", "3"), new XElement("firstName", "Armando"), new XElement("firstName", "Armando"),

new XElement("lastName", "Valdes") ) );new XElement("lastName", "Valdes") ) );

Page 12: LINQ Providers

LINQ to XMLLINQ to XMLLINQ to XMLLINQ to XML

• Result from previous example:Result from previous example:• Result from previous example:Result from previous example:

<contacts> <contacts> <contact contactId="2"> <contact contactId="2">

<firstName>Barry</firstName> <firstName>Barry</firstName> <lastName>Gottshall</lastName> <lastName>Gottshall</lastName>

</contact> </contact> <contact contactId="3"> <contact contactId="3">

<firstName>Armando</firstName> <firstName>Armando</firstName> <lastName>Valdes</lastName> <lastName>Valdes</lastName>

</contact> </contact> </contacts></contacts>

Page 13: LINQ Providers

LINQ to DataSet(s)LINQ to DataSet(s)LINQ to DataSet(s)LINQ to DataSet(s)

• Allows developers to query existing DataSet sources Allows developers to query existing DataSet sources within applicationswithin applications

• Offline, still uses IQueryable<T> approachOffline, still uses IQueryable<T> approach

• 2 types of queries, depending on use of typed/un-2 types of queries, depending on use of typed/un-typed datasetstyped datasets

• Use LINQ in current applications without re-writing Use LINQ in current applications without re-writing using LINQ to SQLusing LINQ to SQL

• Bi-directional: LINQ Sequence -> DataTable -> LINQ Bi-directional: LINQ Sequence -> DataTable -> LINQ Sequence supportSequence support

• Allows developers to query existing DataSet sources Allows developers to query existing DataSet sources within applicationswithin applications

• Offline, still uses IQueryable<T> approachOffline, still uses IQueryable<T> approach

• 2 types of queries, depending on use of typed/un-2 types of queries, depending on use of typed/un-typed datasetstyped datasets

• Use LINQ in current applications without re-writing Use LINQ in current applications without re-writing using LINQ to SQLusing LINQ to SQL

• Bi-directional: LINQ Sequence -> DataTable -> LINQ Bi-directional: LINQ Sequence -> DataTable -> LINQ Sequence supportSequence support

Page 14: LINQ Providers

How Does it Work?How Does it Work?How Does it Work?How Does it Work?

• Depends on if the type is {0} or notDepends on if the type is {0} or not

• IQueryable<T>IQueryable<T>• System.Linq.Queryable (System.Data.Linq.DataQuery<T>)System.Linq.Queryable (System.Data.Linq.DataQuery<T>)

• SQLSQL

• EntityEntity

• DatasetsDatasets

• The extension methods build an expression treeThe extension methods build an expression tree

• If not, then check if it is a IEnumerable<T>If not, then check if it is a IEnumerable<T>• System.Linq.IEnumerable<T>System.Linq.IEnumerable<T>

• ObjectsObjects

• XMLXML

• The extension methods run the queryThe extension methods run the query

• Depends on if the type is {0} or notDepends on if the type is {0} or not

• IQueryable<T>IQueryable<T>• System.Linq.Queryable (System.Data.Linq.DataQuery<T>)System.Linq.Queryable (System.Data.Linq.DataQuery<T>)

• SQLSQL

• EntityEntity

• DatasetsDatasets

• The extension methods build an expression treeThe extension methods build an expression tree

• If not, then check if it is a IEnumerable<T>If not, then check if it is a IEnumerable<T>• System.Linq.IEnumerable<T>System.Linq.IEnumerable<T>

• ObjectsObjects

• XMLXML

• The extension methods run the queryThe extension methods run the query

Page 15: LINQ Providers

How Does it Work?How Does it Work?How Does it Work?How Does it Work?

ObjectsObjects

<book><book> <title/><title/> <author/><author/> <year/><year/> <price/><price/></book></book>

XMLXML

.NET Language Integrated Query.NET Language Integrated Query

C# 3.0C# 3.0 VB 9.0VB 9.0 Others…Others…

RelationalRelational

LINQ toLINQ toObjectsObjects

LINQ toLINQ toSQLSQL

LINQ toLINQ toXMLXML

LINQ toLINQ toEntitiesEntities

LINQ toLINQ toDatasetsDatasets

Page 16: LINQ Providers

How Does it Work?How Does it Work?How Does it Work?How Does it Work?

• Extension methods for the from/select/where/groupby Extension methods for the from/select/where/groupby keywordskeywords

• Lambda expressionsLambda expressions

• Expression tree for building & pre-compiling the Expression tree for building & pre-compiling the statement that will run on a IQueryable<T>statement that will run on a IQueryable<T>• p=> p.CompanyName.StartsWith(“A”)p=> p.CompanyName.StartsWith(“A”)

• Iterators for the in-memory queries (IEnumerable<T>)Iterators for the in-memory queries (IEnumerable<T>)• foreach on an IEnumerable<T>foreach on an IEnumerable<T>

• Extension methods for the from/select/where/groupby Extension methods for the from/select/where/groupby keywordskeywords

• Lambda expressionsLambda expressions

• Expression tree for building & pre-compiling the Expression tree for building & pre-compiling the statement that will run on a IQueryable<T>statement that will run on a IQueryable<T>• p=> p.CompanyName.StartsWith(“A”)p=> p.CompanyName.StartsWith(“A”)

• Iterators for the in-memory queries (IEnumerable<T>)Iterators for the in-memory queries (IEnumerable<T>)• foreach on an IEnumerable<T>foreach on an IEnumerable<T>

Page 17: LINQ Providers

What’s Next?What’s Next?What’s Next?What’s Next?

• PLINQPLINQ

• Parallel FX LibraryParallel FX Library

• IParallelEnumerable<T>IParallelEnumerable<T>

• CTP is availableCTP is available

• Support for 3Support for 3rdrd party DB engines party DB engines

• PLINQPLINQ

• Parallel FX LibraryParallel FX Library

• IParallelEnumerable<T>IParallelEnumerable<T>

• CTP is availableCTP is available

• Support for 3Support for 3rdrd party DB engines party DB engines