210 likes | 443 Views
Using LINQ with .NET Compact Framework. Manav Gaur Program Manager Microsoft India. Agenda. Overview LINQ Architecture LINQ Queries Demos Objects XML Dataset Questions & Answers. Today’s Software Development. Data is fragmented Objects XML Relational Data != Object
E N D
Using LINQ with .NET Compact Framework Manav Gaur Program Manager Microsoft India
Agenda • Overview • LINQ Architecture • LINQ Queries • Demos • Objects • XML • Dataset • Questions & Answers
Today’s Software Development • Data is fragmented • Objects • XML • Relational • Data != Object • Different programming models for each • Imperative programming—describes how, not what to do
.NET Language-Integrated Query • General-purpose query processor built right into your favorite programming language. • Flexible • Extensible • SQL like syntax • LINQ defines standard query operators that allow • Traversal • Filter • Projection Operations • Easy to use for increased productivity • Type safe • Syntax check • IntelliSense support • Terse declarative style—define what to do, not how to do it • Unifies various types of data access
LINQ Architecture on .NETCF C# VB.NET Others… .NET Language Integrated Query (LINQ) LINQ enabled data sources LINQ enabled ADO.NET LINQ To DataSet LINQ To Entities LINQ To SQL LINQ To XML LINQ To Objects Relational <book> <title/> <author/> <price/> </book> Objects XML
LINQ Queries Local variable type inference Filter Conditions Results Ordering IEnumerable<Customer> q = from c in customers select c; var q = from c in customers select c; var q = from c in customers where c.City == “Mumbai” select c; var q = from c in customers where c.City == “Mumbai” orderbyc.Name select c;
More LINQ Queries Anonymous Type Object Initialization Lambda Expression Extension Methods var q = from c in customers where c.City == “Mumbai” orderbyc.Name select new {c.Name, c.Phone}; var q = customers .Where (c => c.City == “Mumbai”) .Orderby(c => c.Name) .Select ( c => new {c.Name, c.Phone});
Query Expressions Zero or more from, join, let, where, or orderby Starts with from fromidinsource { fromidinsource | joinidinsourceonexprequalsexpr [ intoid ] | letid = expr | wherecondition | orderbyordering, ordering, … } selectexpr | groupexprbykey [ intoidquery ] Use let to declare temporary variables Ends with selector group by Optional into continuation
LINQ to Objects Operates on any IEnumerable<T> collection Wide range of operators available Operators can be added by developers
Days Before LINQ… JAIPUR MUMBAI NAGPUR using System; using System.Collections; using System.Collections.Generic; class app { static void Main() { string[] cities = {“Delhi", “Mumbai", “Pune", “Nagpur", “Chennai", “Bangalore", “Jaipur" }; List<String> list = new List<String>(); foreach (string s in cities) if (s.Length == 6) list.Add(s.ToUpper()); list.Sort(); foreach (string s in list) Console.WriteLine(s); } }
And Now, with LINQ using System; using System.Linq; using System.Collections.Generic; class app { static void Main() { string[] cities ={ “Delhi, “Mumbai, “Pune”, “Nagpur”, “Chennai”, “Bangalore, “Jaipur” }; IEnumerable<string> expr = from s in cities wheres.Length == 6 orderby s selects.ToUpper(); foreach (string item in expr) Console.WriteLine(item); } } JAIPUR MUMBAI NAGPUR
LINQ to XML New XML API Standard query operations against XML elements Compact, fast, and uses less memory Create XML easily, use LINQ query output Transform XML
Before LINQ XmlDocument doc = new XmlDocument(); XmlElement books = doc.CreateElement("Books"); XmlElement book = doc.CreateElement("Book"); XmlElement title = doc.CreateElement("Title"); XmlElement price = doc.CreateElement("Price"); doc.AppendChild(books); books.AppendChild(book); book.AppendChild(title); book.AppendChild(price); title.AppendChild( doc.CreateTextNode("Short DOM reference, vol.5")); price.AppendChild(doc.CreateTextNode("99.95")); doc.Save(“MyBooks.xml”); <Books> <Book> <Title>Short DOM reference, vol.5</Title> <Price>99.95</Price> </Book> </Books>
After LINQ XDocument doc = new XDocument( new XElement("Books", new XElement("Book", new XElement("Title", "What to do in spare time freed up from XML programming."), new XElement("Price", "19.95") ) ) ) doc.Save(“MyBooks.xml”); <Books> <Book> <Title>What to do in spare time freed up from XML programming.</Title> <Price>19.95</Price> </Book> </Books>
LINQ to DataSet • Run standard query operators over DataSet • Reuse and extend your existing ADO.NET-based code • Do hard things—like joining tables—easily
Summary • Powerful and convenient language innovations • Easy-to-use expressions • Compile-time type and error checking, IntelliSense • New modern, redesigned XML API • Easier and lighter than DOM • Unified query model for objects, XML and relational data sources
Call to Action • Install and use Visual Studio code name “Orcas” Beta 1 • .NET Compact Framework v3.5 Beta 1 • .NET Compact Framework v2 SP2 • Download and use v2 SP2 in Visual Studio 2005 • Download 101 LINQ Samples • http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx • Send us your feedback • http://connect.microsoft.com/VisualStudio/Feedback • http://blogs.msdn.com/vsdteam • http://blogs.msdn.com/mgaur • mgaur@microsoft.com
© 2007 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. Microsoft makes no warranties, express or implied, in this summary.