Slides from Gang Luo, Xuting Zhao and Damien Guard

Slides:



Advertisements
Similar presentations
Developer Knowledge Sharing Eric Sun Dec, What programming language did you learn in school and since then? Now, its time to refresh …
Advertisements

The Microsoft Technical Roadshow 2007 Language Enhancements and LINQ Daniel Moth Developer & Platform Group Microsoft Ltd
Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training
Michael Pizzo Software Architect Data Programmability Microsoft Corporation.
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
LinqToSharePoint SandBoxed Solution Shakir Majeed Khan
Damien Guard (BSc, MBCS) Guernsey Software Developer Forum Language Integrated Query:
Road Map Introduction to object oriented programming. Classes
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
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.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
 Introduction  What is LINQ  Syntax  How to Query  Example Program.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Getting familiar with LINQ to Objects Florin−Tudor Cristea, Microsoft Student Partner.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
Intro to C#.net and EF Ilan Shimshoni. The Three Faces of ADO.NET The connected layer – Directly connecting to the DB The disconnected layer – Using datasets.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
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.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
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.
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.
© Satyam Computer Services Ltd POC Architecture 1 Confidential – Limited Circulation.
 Language Integrated Query  Make query a part of the language  Component of.NET Framework 3.5  Shipped with Visual Studio 2008.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
 Although VERY commonly used, arrays have limited capabilities  A List is similar to an array but provides additional functionality, such as dynamic.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
C# 3.0 and LINQ Pavel Yosifovich CTO, Hi-Tech College
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
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
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections UTPA – Fall 2011.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
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:
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
LINQ Language Integrated Query LINQ1. LINQ: Why and what? Problem Many data sources: Relational databases, XML, in-memory data structures, objects, etc.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Chapter 11.  Large amounts of data are often stored in a database—an organized collection of data.  A database management system (DBMS) provides mechanisms.
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
Introduction to Entity framework
ASP.NET Programming with C# and SQL Server First Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition
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
Functional Programming with Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Data Model.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
LINQ - 2 Ravi Kumar C++/C# Team.
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

Slides from Gang Luo, Xuting Zhao and Damien Guard LINQ Slides from Gang Luo, Xuting Zhao and Damien Guard

What is LINQ? Language Integrated Query Make query a part of the language Component of .NET Framework 3.5 Key feature of C# 3.0 and VB 9

Query without LINQ Objects using loops and conditions foreach (Customer c in customers) if (c.Region == "UK") ... Databases using SQL SELECT * FROM Customers WHERE Region='UK' XML using XPath/XQuery //Customers/Customer[@Region='UK'] This is the situation today, a different language for each technology.

ADO without LINQ SqlConnection con = new SqlConnection(...); con.Open(); SqlCommand cmd = new SqlCommand( @"SELECT * FROM Customers WHERE c.Region = @Region", con ); cmd.Parameters.AddWithValue("@Region", "UK"); DataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string name = dr.GetString(dr.GetOrdinal("Name")); string phone = dr.GetString(dr.GetOrdinal("Phone")); DateTime date = dr.GetDateTime(3); } dr.Close(); con.Close(); Hard to write database-neutral SQL because of subtle variances in the parameters, escaping, types etc.

Query with LINQ C# var myCustomers = from c in customers where c.Region == "UK" select c; This is the same query in LINQ. You'll notice that the syntax is similar to SQL but with the select last. This is to facilitate IntelliSense - it needs to know what you are selecting from before it can offer what is available within that selection.

More LINQ queries var goodCusts = (from c in db.Customers where c.PostCode.StartsWith("GY") orderby c.Sales descending select c).Skip(10).Take(10);

Local variable type inference Compiler can infer types from initializer assignments var keyword indicates compiler inferred type Still strong-typed This is not like JavaScript var Essential when using anonymous types var a = 10; // Simple types var x = new { Blog = “attardi”, Created = DateTime.Now }; // Anonymous types

Anonymous Types Object of new type generated on the fly without first defining it. Useful for projection to select one or more fields of another structure. The type will be dynamically generated with setters and getters to corresponding members. Some common methods are also provided. No other methods will be added to this type. But that is already enough! The object is created and initialized by Anonymous Object Initializer.

Advantages Unified data access Single syntax to learn and remember Strongly typed Catch errors during compilation IntelliSense Prompt for syntax and attributes Bindable result sets

Architecture

LINQ to Objects int[] nums = new int[] {0,4,2,6,3,8,3,1}; double average = nums.Take(6).Average(); var above = from n in nums where n > average select n;

LINQ to Objects Query any IEnumerable<T> source Includes arrays, List<T>, Dictionary... Many useful operators available Sum, Max, Min, Distinct, Intersect, Union Expose your own data with IEnumerable<T> or IQueryable<T> Create operators using extension methods

LINQ operators Aggregate Conversion Ordering Partitioning Sets Aggregate Average Count Max Min Sum Cast OfType ToArray ToDictionary ToList ToLookup ToSequence OrderBy ThenBy Descending Reverse Skip SkipWhile Take TakeWhile Concat Distinct Except Intersect Union and others …

Query Expression SQL-like: OO-style: from s in names where s.Length == 5 orderby select s.ToUpper(); OO-style: names.Where(s => s.Length==5) .OrderBy(s => s) .Select(s => s.ToUpper()); Where, OrderBy, and Select are operators. The arguments to these operators are Lambda Expression.

Lambda Expressions Examples: Executable function Expression Tree s => s.Length == 5 Executable function Anonymous functional. Can be assigned to a delegate variable. No need to indicate the types Can be passed to methods as parameters. Expression Tree Efficient in-memory data representations of lambda expressions Changing the behaviors of the expressions Applying your own optimization

Function Types Func<int, bool> is a shorthand for public delegate bool Func(int a0); // Initialized with anonymous method Func<int, bool> even= delegate (int x) { return x % 2 == 0; }; // Initialized with lambda expression Func<int, bool> even2 = x => x % 2 == 0;

Methods Extension Control not only by Lambda Expression, but also by methods extension public static class Enumerable { public static IEnumerable<T> Where<T>( this IEnumerable<T> source, Func<T, bool> predicate) { foreach (T item in source) if (predicate(item)) yield return item; }

LINQ Operations Join When there is relationship (e.g. foreign key) between two tables, no explicit join operation is needed Using dot notation to access the relationship properties, and navigate all the matching objects. var q = from o in db.Orders where o.Customer.City == “London” select o; To join any two data sources on any attribute, you need an explicit join operation. var query = names.Join(people, n => n, p => p.Name, (n, p) => p); The lambda expression for shaping (n, p) => p will be applied on each matching pairs.

LINQ Operations (cont.)  Group Join The lambda expression for shaping is applied on the outer element and the set of all the inner elements that matches the outer one. Shape the result at a set level var query = names.GroupJoin(people, n => n, p => p.Name, (n, matching) => new { Name = n, Count = matching.Count() } )

LINQ Operations (cont.) Select Many Each object in the result set may contain a collection or array Select many help decompose the structure and flatten the result var query = names.SelectMany(n => people.Where(p => n == p.Name)) All the elements could be traversed in one foreach loop. Aggregation Standard aggregation operators: Min, Max, Sum, Average. int totalLength=names.Sum(s => s.Length); General purpose (generic) operator: static U Aggregate<T, U>(this IEnumerable<T> source, U seed, Func<U, T, U> func)

LINQ Deferred A LINQ data source can actually implement one of two interfaces: IEnumerable<T> IQueryable<T> Create deferred query execution plan public interface IQueryable<T> : IEnumerable<T>, IQueryable, IEnumerable { IQueryable<S> CreateQuery<S>(Expression exp); S Execute<S>(Expression exp); }

IQueryable IQueryable<T> interface will defer the evaluation of the query. An expression tree will represent all the deferred queries as a whole. Several operations could be “merged”, only one SQL query will be generated and sent to database. Multi-level defer 

Lambda Expressions Revisited Lambda expressionscan represent either IL code or data Expression<T> makes the difference Compiler handles Expression<T> types differently Emits code to generate expression tree instead of usual IL for delegate Func<int, bool> lambdaIL = n => n % 2 == 0; Expression<Func<int, bool>> lambdaTree =

Expression Trees Expression tree are hierarchical trees of instructions that compose an expression Add value of expression trees Actual creating of IL is deferred until execution of query Implementation of IL creation can vary Trees can even be remoted for parallel processing

Creating IL from Expression Trees Right before execution tree is compiled into IL Implementation of IL generation differs very much for each Linq flavor. Linq to SQL generates IL that runs SQL commands Linq to Objects builds IL with Sequence extensions methods Expression<Func<Posting,bool>> predicate = p => p.Posted < DateTime.Now.AddDays(-5); Func<Posting,bool> d = predicate.Compile();

Nested defer Nested defer What if you want the intermediate result? var q = from c in db.Customers where c.City == “London” select new { c.ContactName, c.Phone } var q2 = from c in q.AsEnumerable() select new { Name = DoNameProcessing(c.ContactName), Phone = DoPhoneProcessing(C.Phone) }; string lastName = “Simpson” var persons = from p in personList where p.LastName = lastName select p; lastName = “Flanders” foreach (Person p in persons) Console.WriteLine(“{0} {1}”, p.FirstName, p.LastName);

Deferred Execution Advantages Disadvantages Performance! Query dependency! Disadvantages Divide one query into multiple ones If you iterate over the result set 100 times, the query will be executed 100 times. Users have to be very careful

LINQ to SQL Data Model LINQ to SQL helps connect to relational and manipulate the relational data as objects in memory. It achieves this by translating the operations into SQL statements. [Table(Name=“Customers”)] public class Customer { [Column(Id=true)] public string CustomerID; … private EntitySet<Order> _Orders; [Association(Storage=“_Orders”, OtherKey=“CustomerID”)] public EntitySet<Order> Orders { get { return _Orders; } set { _Orders.Assign(value); } }

LINQ to SQL Object-relational mapping Records become strongly-typed objects Data context is the controller mechanism Facilitates update, delete & insert Translates LINQ queries behind the scenes Type, parameter and injection safe

Database mapping Map tables & fields to classes & properties Generates partial classes with attributes Each record becomes an object Data context represents the database Utilize tables, views or stored procedures

Modifying objects Update Set object properties Delete context.Table.DeleteOnSubmit(object) Insert context.Table.InsertOnSubmit(object) Commit changes back context.SubmitChanges() Transactional - all or nothing

Consistency Every object will be tracked by LINQ the moment it is loaded from database. The tracking mechanism monitor the manipulation on relationship properties. Once you modify one side of the relationship, LINQ will modify the other to keep it consistent.  When an object is deleted, it could still exist in memory, but it will not cause inconsistency.

Concurrency Optimistic concurrency Conflict checking when SubmitChanges() is called By default, transaction will abort and an exception will be thrown when a conflict is detected. User can handle the conflict in the exception catch block. User can set whether or not to detect the conflict when one column get updated.

Transaction/Update When update, first check whether new object is added (by tracking mechanism) if yes, insert statement will be generated. What does Django do here? Modification will not hit the database until the SubmitChanges() method is called All operations will be translated into SQL statements All modifications will be encapsulated into a transaction.

Transaction/Update (cont.) If an exception is throw during the update, all the changes will be rolled back One SubmitChanges() is actually one transaction. (pros and cons?) Users can also explicitly indicate a new transaction scope.

LINQ to XML Class Hierarchy http://msdn.microsoft.com/en-us/library/bb308960.aspx

LINQ to XML LINQ to XML XML to LINQ var query = from p in people where p.CanCode select new XElement(“Person”, new XAttribute(“Age”, p.Age), p.Name); var x = new XElement(“People”, from p in people where p.CanCode select new XElement(“Person”, new XAttribute(“Age”, p.Age), p.Name);

Performance LINQ has more control and efficiency in O/R Mapping than NHibernate LINQ: Externl Mapping or Attribute Mapping NHibernate: Externl Mapping Because of mapping, LINQ is slower than database tools such as SqlDataReader or SqlDataAdapter In large dataset, their performance are more and more similar