C# 3.0 & LINQ Raimond Brookman – IT Architect

Slides:



Advertisements
Similar presentations
The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Advertisements

Extension Methods, Anonymous Types LINQ Query Keywords, Lambda Expressions Svetlin Nakov Telerik Corporation
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
© Logica All rights reserved ADO vNext LINQ LINQ to SQL Entity Framework Freek Leemhuis
DEV306 LINQ (Language Integrated Query)
Joshua Clark Consultant Sogeti USA Blog:
Live from Redmond: WebCast Conquering XML with Visual Basic 9 Amanda Silver Lead Program Manager Visual Basic Level: Starts Easy, Finishes Advanced.
Overview of Next-gen technologies Raimond Brookman IT Architect Info Support
ASP.NET 3.5 New Features. 2 Agenda What's New in.NET Framework 3.5? Visual Studio 2008 Enhancements LINQ (Language Integrated Query) New ASP.NET Server.
LinqToSharePoint SandBoxed Solution Shakir Majeed Khan
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
1 Agenda Data and System Trends ChallengesObservationsConclusion.
LINQ: Language-Integrated Queries (To be included in C # 3.0) Technology developed by Anders Hejlsberg & friends at Microsoft (2005) Presented by Tal Cohen.
C# 3.0 Tom Roeder CS fa. Version 3 From PDC 2005 preview compiler available LINQ: language-integrated query High level points: adds native query.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
A tour of new features introducing LINQ. Agenda of LINQ Presentation We have features for every step of the way LINQ Fundamentals Anonymous Functions/Lambda.
Beth Massi Program Manager Visual Studio Community.
Paul Yuknewicz Lead Program Manager Microsoft Visual Basic DEV 319.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
Slides from Gang Luo, Xuting Zhao and Damien Guard
Reflection IT LINQ & Entity Framework Fons Sonnemans (Trainer)
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Eric Vogel Software Developer A.J. Boggs & Company.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics LINQ to XML: A Brief Overview Pavel Ježek.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# 3.0 and.NET 3.5: A Brief Overview Pavel Ježek.
C# 3.0 & LINQ 천호민 Visual C# MVP zmeun.tistory.com.
Getting familiar with LINQ to Objects Florin−Tudor Cristea, Microsoft Student Partner.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Лектор: Борислава Палева. Galin Iliev MCT, MCPD, MCSD.NET LINQ to XML.
LINQ and C# 3.0 Mads Torgersen Program Manager for the C# Language Microsoft Corporation.
LINQ: It’s Not Your Father’s Data Access Denny Boynton Anheuser-Busch Companies.
The.NET Language Integrated Query Project Anders Hejlsberg TLN306 Technical Fellow Microsoft Corporation.
1 TPlite is Back! Jim Gray Microsoft Research High Performance Transaction Processing Workshop, Asilomar, CA.,
 Language Integrated Query  Make query a part of the language  Component of.NET Framework 3.5  Shipped with Visual Studio 2008.
LINQ Providers Or why.NET rules, and Java doesn’t Branimir Giurov SofiaDev.org UG Lead, C# MVP Freelance Software Developer
Välkommen till Sommarkollo VISUAL STUDIO 2008.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
AUC Technologies LINQ (Language Integrated Query) LINQ Presented By : SHAIKH SHARYAR JAVED Software Engineer (Daedalus Software Inc.) Technology Teacher.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
Applied Linq Putting Linq to work Introducing… Class-A Kennisprovider Microsoft development Training Coaching Alex Thissen Trainer/coach.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
Linq Overview Vincent GERMAIN. Evolution - Rappel Langage  C# 2.0  C# 3.0 (Local type inference, Lambda expression, Method extension,Anonymous type)
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
Joel Pobar Language Geek Microsoft DEV320 Improve on C# % Backwards Compatible Language Integrated Query (LINQ)
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
Ken Casada Developer Evangelist Microsoft Switzerland
Part 1: Overview of LINQ Intro to LINQ Presenter: PhuongNQK.
Introduction to Entity Framework
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2016
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2013
Intro to LINQ Part 2 – Concepts and LINQ to Objects
Language Integrated Query: (LINQ) An introduction
Advanced .NET Programming I 6th Lecture
Language Integrated Query (LINQ)
Visual Studio “Orcas” & .NET Framework v3.5
Language Integrated Query (LINQ)
LINQ & ADO.NET Entity Framework
INFO 344 Web Tools And Development
Технологии доступа к данным на платформе Microsoft.NET
LINQ - 2 Ravi Kumar C++/C# Team.
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 6th Lecture
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

C# 3.0 & LINQ Raimond Brookman – IT Architect

Outline -Linq Project -DLinq -XLinq -C# 3.0 Language Enhancements

Problem: Data != Objects

Service based Solution Customer Mgt Service (HTTP + XML) WebApplication Order Mgt WS Order Processing WS ORM Xml R/W Databound grid BR Check Transform Msg XML Config Xml Read

The LINQ Project Standard Query Operators Objects DLinq (ADO.NET) XLinq(System.Xml) XML.NET Language Integrated Query C# 3.0 VB 9.0 Others… SQLWinFS

Service based Solution with LINQ Customer Mgt Service (HTTP + XML) WebApplication Order Mgt WS Order Processing WS ORM Xml R/W Databound grid BR Check Transform Msg XML Config Xml Read DLINQ O-LINQ XLINQ O-LINQ

LINQ Style programming var contacts = from c in customers from c in customers where c.State == "WA" where c.State == "WA" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; class Contact { … }; List contacts = new List (); foreach(Customer c in customers) { if(c.State == “WA”) { Contact ct = new Contact(); Contact ct = new Contact(); ct.Name = c.Name; ct.Name = c.Name; ct.Phone = c.Phone; ct.Phone = c.Phone; contacts.Add(ct); contacts.Add(ct);}}

Query Expressions Language integrated query syntax from id in source { from id in source | where condition } [ orderby ordering, ordering, … ] select expr | group expr by key [ into id query ]

Standard Query Operators RestrictionWhere Projection Select, SelectMany Ordering OrderBy, ThenBy GroupingGroupBy Quantifiers Any, All Partitioning Take, Skip, TakeWhile, SkipWhile Sets Distinct, Union, Intersect, Except Elements First, FirstOrDefault, ElementAt Aggregation Count, Sum, Min, Max, Average Conversion ToArray, ToList, ToDictionary CastingOfType<T>

DLinq For Relational Data SqlConnection c = new SqlConnection(…); c.Open(); SqlCommand cmd = new c.Name, c.Name, c.Phone FROM Customers c FROM Customers c WHERE c.City WHERE c.City "London“); DataReader dr = c.Execute(cmd); while (dr.Read()) { string name = dr.GetString(0); string name = dr.GetString(0); string phone = dr.GetString(1); string phone = dr.GetString(1); DateTime date = dr.GetDateTime(2); DateTime date = dr.GetDateTime(2);}dr.Close(); Accessing data today Queries in quotes Loosely bound arguments Loosely typed result sets No compile time checks

public class Customer { … } public class Northwind: DataContext { public Table Customers; public Table Customers; …} Northwind db = new Northwind(…); var contacts = from c in db.Customers from 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 Data Accessing data with DLinq Classes describe data Strongly typed connection Integrated query syntax Strongly typed results Tables are like collections

DLinq For Relational Data Language integrated data access Maps tables and rows to classes and objects Builds on ADO.NET and.NET Transactions Mapping Encoded in attributes Relationships map to properties Persistence Automatic change tracking Updates through SQL or stored procedures

XLinq For XML Data XmlDocument doc = new XmlDocument(); XmlElement contacts = doc.CreateElement("contacts"); 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); Programming XML today Great Lakes Food (503) … Imperativemodel Documentcentric No integrated queries Memoryintensive

XLinq For XML Data XElement contacts = new XElement("contacts", from c in customers from 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 XLinq Declarativemodel Element centric Integratedqueries Smaller and faster

XLinq For XML Data Language integrated query for XML Expressive power of XPath / XQuery But with C# or VB as programming language Leverages experience with DOM Element centric, not document centric Functional construction Text nodes are just strings Simplified XML namespace support Faster and smaller

C# 3.0 Design Goals Integrate objects, relational, and XML Build on foundation laid in C# 1.0 and 2.0 Run on the.NET 2.0 CLR Remain 100% backwards compatible

C# 3.0 Language Innovations var contacts = from c in customers from c in customers where c.State == "WA" where c.State == "WA" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; var contacts = customers customers.Where(c => c.State == "WA").Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone });.Select(c => new { c.Name, c.Phone }); Extensionmethods Lambdaexpressions Queryexpressions Objectinitializers Anonymoustypes Local variable type inference

Queries Through APIs public class List public class List { public List Where(Func predicate) { … } public List Where(Func predicate) { … } public List Select (Func selector) { … } public List Select (Func selector) { … } …} List customers = GetCustomerList(); List contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); customers.Where(c => c.State == "WA").Select(c => c.Name); Query operators are just methods But what about other types? Declare operators in all collections? What about arrays? Methods compose to form queries

Queries Through APIs public static class Sequence { public static IEnumerable Where (IEnumerable source, public static IEnumerable Where (IEnumerable source, Func predicate) { … } Func predicate) { … } public static IEnumerable Select (IEnumerable source, public static IEnumerable Select (IEnumerable source, Func selector) { … } Func selector) { … } …} Customer[] customers = GetCustomerArray(); IEnumerable contacts = Sequence.Select( Sequence.Where(customers, c => c.State == "WA"), Sequence.Where(customers, c => c.State == "WA"), c => c.Name); c => c.Name); Query operators are static methods Huh? Want methods on IEnumerable<T>

namespace System.Query { public static class Sequence public static class Sequence { public static IEnumerable Where (this IEnumerable source, public static IEnumerable Where (this IEnumerable source, Func predicate) { … } Func predicate) { … } public static IEnumerable Select (this IEnumerable source, public static IEnumerable Select (this IEnumerable source, Func selector) { … } Func selector) { … } … }} Solution: Extension Methods using System.Query; Extension methods IEnumerable contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); customers.Where(c => c.State == "WA").Select(c => c.Name); Brings extensions into scope obj.Foo(x, y)  XXX.Foo(obj, x, y) IntelliSense!

Local Variable Type Inference int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary orders = new Dictionary (); var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary (); “var” means same type as initializer

Anonymous Types public class Customer { public string Name; public string Name; public Address Address; public Address Address; public string Phone; public string Phone; public List Orders; public List Orders; …} public class Contact { public string Name; public string Name; public string Phone; public string Phone;} Customer c = GetCustomer(…); Contact x = new Contact { Name = c.Name, Phone = c.Phone }; Customer c = GetCustomer(…); var x = new { c.Name, c.Phone }; Customer c = GetCustomer(…); var x = new { Name = c.Name, Phone = c.Phone }; class ??? { public string Name; public string Name; public string Phone; public string Phone;} Projection style initializer

var contacts = from c in customers from c in customers where c.State == "WA" where c.State == "WA" select new { c.Name, c.Phone }; select new { c.Name, c.Phone }; Anonymous Types var contacts = customers. customers..Where(c => c.State == "WA“).Where(c => c.State == "WA“).Select(c => new { c.Name, c.Phone });.Select(c => new { c.Name, c.Phone }); class ??? { public string Name; public string Name; public string Phone; public string Phone;} IEnumerable<???> foreach (var c in contacts) { Console.WriteLine(c.Name); Console.WriteLine(c.Name); Console.WriteLine(c.Phone); Console.WriteLine(c.Phone);} ???

public delegate bool Predicate (T obj); public class List public class List { public List FindAll(Predicate test) { … } public List FindAll(Predicate test) { … } …} Lambda Expressions List customers = GetCustomerList(); List x = customers.FindAll( delegate(Customer c) { return c.State == "WA"; } delegate(Customer c) { return c.State == "WA"; }); List x = customers.FindAll(c => c.State == "WA"); Explicitly typed Statementcontext Implicitly typed Expressioncontext

Expression Trees public class Northwind: DataContext { public Table Customers; public Table Customers; public Table Orders; public Table Orders; …} Northwind db = new Northwind(…); var query = from c in db.Customers where c.State == "WA" select c; Northwind db = new Northwind(…); var query = db.Customers.Where(c => c.State == "WA"); How does this become SQL ? public class Table : IEnumerable public class Table : IEnumerable { public Table Where(Expression > predicate); public Table Where(Expression > predicate); …} Method asks for expression tree System.Expressions. Expression System.Expressions. Expression

Expression Trees Code as Data Func test = c => c.State == "WA"; Expression > test = c => c.State == "WA"; ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.EQ( Expression.EQ( Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Constant("WA") Expression.Constant("WA") ); ); Expression > test = Expression.Lambda >(expr, c); Expression.Lambda >(expr, c);

Expression Trees Code as Data Func test = c => c.State == "WA"; Expression > test = c => c.State == "WA"; ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.EQ( Expression.EQ( Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Constant("WA") Expression.Constant("WA") ); ); Expression > test = Expression.Lambda >(expr, c); Expression.Lambda >(expr, c);

Benefits Of LINQ Unified querying of objects, relational, XML Type checking and IntelliSense for queries SQL and XQuery-like power in C# and VB Extensibility model for languages / APIs

Call to Action -Get VS Download LINQ preview Play Around & See the power!

Q & A