Post on 01-Jan-2016
LINQ and C# 3.0LINQ and C# 3.0
Mads TorgersenMads TorgersenProgram Manager for the C# LanguageProgram Manager for the C# LanguageMicrosoft CorporationMicrosoft Corporation
fromfrom c c inin db.Customers db.Customerswherewhere c.City == “London” c.City == “London”select newselect new { c.Name, c.Address } { c.Name, c.Address }
LINQ ArchitectureLINQ Architecture
ObjectsObjects
<book> <title/> <author/> <price/></book>
XMLXMLRelationalRelational
LINQ enabled data sourcesLINQ enabled data sources
LINQ LINQ To ObjectsTo Objects
LINQ LINQ To XMLTo XML
LINQ enabled ADO.NETLINQ enabled ADO.NET
VBVB Others…Others…
LINQ LINQ To EntitiesTo Entities
LINQ LINQ To SQLTo SQL
LINQ LINQ To DatasetsTo Datasets
.Net Language Integrated Query (LINQ).Net Language Integrated Query (LINQ)
C#C#
C# 3.0 Language ExtensionsC# 3.0 Language Extensions
var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone };
var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone });
Extension Extension methodsmethods
Lambda Lambda expressionsexpressions
Query Query expressionsexpressions
Object Object initializersinitializers
Anonymous Anonymous typestypes
Local Local variable type variable type
inferenceinference
Language Integrated QueryLanguage Integrated Query
Querying in Visual C# 3.0Querying in Visual C# 3.0
Extension methodsExtension methods – call static as – call static as instance instance
Lambda expressionsLambda expressions – inline methods – inline methods
Query expressionsQuery expressions – querying made – querying made simplesimple
Implicitly typed locals – Implicitly typed locals – save the save the typingtyping
Anonymous typesAnonymous types – temporary results – temporary results
Standard Query OperatorsStandard Query OperatorsRestrictRestrict s.s.WhereWhere(…)(…)
ProjectProject s.s.SelectSelect(…), s.(…), s.SelectManySelectMany(…)(…)
OrderOrder s.s.OrderByOrderBy(…).(…).ThenByThenBy(…) …(…) …
GroupGroup s.s.GroupByGroupBy(…)(…)
QuantifyQuantify s.s.AnyAny(…), s.(…), s.AllAll(…)(…)
PartitionPartition s.s.TakeFirstTakeFirst(…), s.(…), s.SkipFirstSkipFirst(…)(…)
SetSet s.s.DistinctDistinct(), s.(), s.UnionUnion(…), s.(…), s.IntersectIntersect(…), (…), s.s.ExceptExcept(…)(…)
SingletonSingleton s.s.ElementElement(…), s.(…), s.ElementAtElementAt(…)(…)
AggregatAggregatee
s.s.CountCount(), s.(), s.SumSum(), s.(), s.MinMin(), s.(), s.MaxMax(), (), s.s.AverageAverage(), s.(), s.AggregateAggregate()()
ConvertConvert s.s.ToArrayToArray(), s.(), s.ToListToList()()
CastCast s.s.OfTypeOfType<T>(), s.<T>(), s.CastCast<T><T>
DLinq for Relational DataDLinq for Relational Data
SqlConnection c = new SqlConnection(…);SqlConnection c = new SqlConnection(…);c.Open();c.Open();SqlCommand cmd = new SqlCommand(SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone@"SELECT c.Name, c.Phone FROM Customers cFROM Customers c WHERE c.City = @p0");WHERE c.City = @p0");cmd.Parameters["@p0"] = "London";cmd.Parameters["@p0"] = "London";DataReader dr = c.Execute(cmd);DataReader dr = c.Execute(cmd);while (dr.Read()) {while (dr.Read()) { string name = r.GetString(0);string name = r.GetString(0); string phone = r.GetString(1);string phone = r.GetString(1); DateTime date = r.GetDateTime(2);DateTime date = r.GetDateTime(2);}}r.Close();r.Close();
Accessing data todayAccessing data today
Queries in Queries in quotesquotes
Loosely Loosely bound bound
argumentsarguments
Loosely Loosely typed result typed result
setssets
No compile No compile time checkstime checks
public class Customer { … }public class Customer { … }
public class Northwind: DataContextpublic class Northwind: DataContext{{ public Table<Customer> Customers;public Table<Customer> Customers; … …}}
Northwind db = new Northwind(…);Northwind db = new Northwind(…);var contacts =var contacts = from c in db.Customersfrom c in db.Customers where c.City == "London"where c.City == "London" select new { c.Name, c.Phone };select new { c.Name, c.Phone };
DLinq for Relational DataDLinq for Relational Data
Accessing data with DLinqAccessing data with DLinqClasses Classes
describe datadescribe data
Strongly Strongly typed typed
connectionconnection
Integrated Integrated query syntaxquery syntax
Strongly Strongly typed resultstyped results
Tables are Tables are like like
collectionscollections
XLinq for XML DataXLinq for XML Data
XmlDocument doc = new XmlDocument();XmlDocument doc = new XmlDocument();XmlElement contacts = doc.CreateElement("contacts");XmlElement contacts = doc.CreateElement("contacts");foreach (Customer c in customers)foreach (Customer c in customers) if (c.Country == "USA") {if (c.Country == "USA") { XmlElement e = doc.CreateElement("contact");XmlElement e = doc.CreateElement("contact"); XmlElement name = doc.CreateElement("name");XmlElement name = doc.CreateElement("name"); name.InnerText = c.CompanyName;name.InnerText = c.CompanyName; e.AppendChild(name);e.AppendChild(name); XmlElement phone = doc.CreateElement("phone");XmlElement phone = doc.CreateElement("phone"); phone.InnerText = c.Phone;phone.InnerText = c.Phone; e.AppendChild(phone);e.AppendChild(phone); contacts.AppendChild(e);contacts.AppendChild(e); }}doc.AppendChild(contacts);doc.AppendChild(contacts);
Programming XML todayProgramming XML today
<contacts><contacts> <contact><contact> <name>Great Lakes <name>Great Lakes Food</name>Food</name> <phone>(503) <phone>(503) 555-7123</phone>555-7123</phone> </contact></contact> … …</contacts></contacts>
Imperative Imperative modelmodel
Document Document centriccentric
No integrated No integrated queriesqueries
Memory Memory intensiveintensive
XLinq for XML DataXLinq for XML Data
XElement contacts = new XElement("contacts",XElement contacts = new XElement("contacts", from c in customersfrom c in customers where c.Country == "USA"where c.Country == "USA" select new XElement("contact",select new XElement("contact", new XElement("name", c.CompanyName),new XElement("name", c.CompanyName), new XElement("phone", c.Phone)new XElement("phone", c.Phone) ))););
Programming XML with XLinqProgramming XML with XLinqDeclarative Declarative
modelmodel
ElementElementcentriccentric
Integrated Integrated queriesqueries
Smaller and Smaller and fasterfaster
LINQ ExtensibilityLINQ Extensibility
New data sourcesNew data sourcesImplement the LINQ patternImplement the LINQ pattern
New languagesNew languagesUnified querying experienceUnified querying experience
Expression tree generationExpression tree generation
Unified call syntaxUnified call syntax
Query syntaxQuery syntax
Try it!Try it!
http://http://msdn.microsoft.com/netframework/future/limsdn.microsoft.com/netframework/future/linqnq
//
© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.
MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.