Transcript IT Boxing Championship
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)
C# 3.0
VB 9.0
Others… .NET Language Integrated Query LINQ to Objects LINQ to Datasets LINQ to SQL LINQ to Entities LINQ to XML Objects Relational
typeof(linq)
• • • • • • • • •
LINQ to Objects LINQ to SQL LINQ to XML LINQ to Datasets LINQ to Entities LINQ to Windows Search LINQ to Google LINQ to Sharepoint LINQ to nHibernate
LINQ to Objects
•
Query language over collections of objects
•
Standard SQL-like set of query operators, including joins and grouping
•
New query operators can be added, fully extensible (extension methods)
•
Intellisense and compiler syntax checking support in Visual Studio
LINQ to Objects
•
How to query an array of int?
public void Linq1() { int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n; Console.WriteLine("Numbers < 5:"); foreach (var x in lowNums) { Console.WriteLine(x); } }
LINQ to SQL
• • • • • •
Write queries in any .NET language to retrieve and manipulate data from SQL Use FK constraints for defining relations of data without joins Join in code on any column that has no FK defined Use FK’s to query/modify data in SQL DB Call SubmitChanges() when you’re done Currently supports SQL 2000 & 2005
•
System.Date.Linq.SqlClient.Sql2000Provider
•
System.Date.Linq.SqlClient.Sql2005Provider
DEMO LINQ to SQL ToString() Modifying Queries Mapping Styles Mapping Tools
LINQ to XML
• •
LINQ to XML is a new way to construct, write and read XML data in the .NET language of the developers’ choice Simplifies working with XML
• • • • •
No more XPath or XSLT Not a replacement for DOM or any of the current XML libs Supports writing Query Expressions Can be combined with any of the other LINQ technologies Supports SAX-style reading/writing of documents with the use of XStreamingElement
•
Differed execution
LINQ to XML
•
Combining it with other LINQ technologies makes life easier XElement xml = new new new new XElement("contacts", XElement("contact", new XAttribute("contactId", "2"), new new XElement("firstName", "Barry"), XElement("lastName", "Gottshall") ), XElement("contact", new XAttribute("contactId", "3"), new XElement("firstName", "Armando"), XElement("lastName", "Valdes") ) );
LINQ to XML
•
Result from previous example:
LINQ to DataSet(s)
• • • • •
Allows developers to query existing DataSet sources within applications Offline, still uses IQueryable
How Does it Work?
•
Depends on if the type is {0} or not
•
IQueryable
• •
System.Linq.Queryable (System.Data.Linq.DataQuery
•
SQL
•
Entity
•
Datasets The extension methods build an expression tree
•
If not, then check if it is a IEnumerable
•
System.Linq.IEnumerable
•
Objects
•
XML
•
The extension methods run the query
How Does it Work?
C# 3.0
VB 9.0
Others… .NET Language Integrated Query LINQ to Objects LINQ to Datasets LINQ to SQL LINQ to Entities LINQ to XML Objects Relational
How Does it Work?
• • • •
Extension methods for the from/select/where/groupby keywords Lambda expressions Expression tree for building & pre-compiling the statement that will run on a IQueryable
•
p=> p.CompanyName.StartsWith
(“A”) Iterators for the in-memory queries (IEnumerable
•
foreach on an IEnumerable
What’s Next?
•
PLINQ
•
Parallel FX Library
•
IParallelEnumerable
•
CTP is available
•
Support for 3 rd party DB engines