CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek

Slides:



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

LINQ and Collections An introduction to LINQ and Collections.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
C# and LINQ Yuan Yu Microsoft Research Silicon Valley.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
C# 3.0 & LINQ Raimond Brookman – IT Architect
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.
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.
LINQ Programming in C# LINQ CSE Prof. Roger Crawfis.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
LINQ, An IntroLINQ, An Intro Florin−Tudor Cristea, Microsoft Student Partner.
Advanced .NET Programming I 13th Lecture
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# 3.0 and.NET 3.5: A Brief Overview Pavel Ježek.
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.
Advanced C#, part IV Niels Hallenberg IT University of Copenhagen (With thanks to Peter Sestoft and Kasper Østerbye) BAAAP – Spring 2009.
Monads Steve Goguen. About Me Web Developer for Allied Building Supply  ASP.NET, SQL Server, LINQ, etc. Website:
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
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.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
1 LinQ Introduction. Outline Goals of LinQ Anatomy of a LinQ query More expression examples LinQ to Objects LinQ to XML LinQ to SQL 2.
Language Integrated Query (LINQ). Data Access Programming Challenges Developers must learn data store-specific query syntax Multiple, disparate data stores.
By: Luis Carranco CIS764 - Fall  What is LINQ  Architecture  How does it work?  Samples/Demo  Why to use LINQ? 2.
C#: Future Directions in Language Innovation Anders Hejlsberg TLN307 Technical Fellow Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
Satisfy Your Technical Curiosity C# 3.0 Raj Pai Group Program Manager Microsoft Corporation
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
LINQ and Lambda Expressions Telerik Software Academy LINQ Overview.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
Luke Hoban Senior Program Manager Microsoft Session Code: DTL319.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek
Functional Programming Data Aggregation and Nested Queries Ivan Yonkov Technical Trainer Software University
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2016
Advanced .NET Programming I 2nd Lecture
Advanced .NET Programming I 3nd Lecture
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2013
Advanced .NET Programming II 6th Lecture
Intro to LINQ Part 2 – Concepts and LINQ to Objects
LINQ  LINQ – Language INtegrated Query  ( заявки вградени в езика )
Advanced .NET Programming I 4th Lecture
Advanced .NET Programming I 6th Lecture
Language Integrated Query (LINQ)
Language Integrated Query (LINQ)
LINQ - 2 Ravi Kumar C++/C# Team.
Advanced .NET Programming I 13th Lecture
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 4th Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 4th Lecture
C# Language & .NET Platform 8th Lecture
C# Language & .NET Platform 12th Lecture
CS4540 Special Topics in Web Development LINQ to Objects
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (

Query Expressions – Examples Query expression: from c in customers where c.City == "London" orderby c.Name select new { c.City, c.Name } Gets translated to: customers.Where(c => c.City == "London").OrderBy(c => c.Name).Select(c => new { c.City, c.Name }) NOTE – is equivalent to (i.e. “c” variables in lambdas are not related to each other): customers.Where(c1 => c1.City == "London").OrderBy(c2 => c2.Name).Select(c3 => new { c3.City, c3.Name })

Query Expressions – Query Ops (MSDN) α let a = b β α.Select(x => new { x, a = b}).β

Query Expressions Query expressions or LINQ (Language INtergrated Queries) are the key feature of.NET 3.5 Query expressions are translated to method calls – works on classes like: delegate R Func (A arg); class C { public C Where(Func predicate); public C Select (Func selector); public C SelectMany (Func > selector); public O OrderBy (Func keyExpr); public O OrderByDescending (Func keyExpr); public C > GroupBy (Func keyExpr); public C > GroupBy (Func keyExpr, Func elemExpr); … }

LINQ to Objects Set of generic extension methods ( Select, Where, OrderBy, etc.) implemented for IEnumerable interface (provided by static class Enumerable ), example: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numberGroups = from n in numbers group n by n % 5 into g select new { Remainder = g.Key, Numbers = g }; foreach (var g in numberGroups) { Console.WriteLine( "Numbers with a remainder of {0} when divided by 5:", g.Remainder ); foreach (int n in g.Numbers) { Console.WriteLine(n); } LINQ to * - Classes for * data access using query expressions NOTE: For any LINQ implementation it is commonly expected (but not enforced [see LinqToNothing example]), that the “resulting” type of any query implements at least the IEnumerable interface. Numbers with a remainder of 0 when divided by 5: 5 0 Numbers with a remainder of 4 when divided by 5: 4 9 Numbers with a remainder of 1 when divided by 5: 1 6 Numbers with a remainder of 3 when divided by 5: 3 8 Numbers with a remainder of 2 when divided by 5: 7 2

Query Expressions – Examples Query expression: from c in customers orderby c.Name orderby c.City select new { c.City, c.Name } Gets translated to: customers.OrderBy(c => c.Name).OrderBy(c => c.City).Select(c => new { c.City, c.Name })

Query Expressions – Examples Query expression: from c in customers orderby c.Name, c.City select new { c.City, c.Name } Gets translated to: customers.OrderBy(c => c.Name).ThenBy(c => c.City).Select(c => new { c.City, c.Name })

Query Expressions – Examples Query expression: from c in customers where c.City == "London" from o in c.Orders where o.OrderDate.Year == 2005 select new { c.Name, o.OrderID, o.Total } Gets translated to: customers.Where(c => c.City == "London"). SelectMany(c => c.Orders. Where(o => o.OrderDate.Year == 2005). Select(o => new { Name = c.Name, OrderID = o.OrderID, Total = o.Total }) )

Query Expressions – Query Ops (MSDN) α let a = b β α.Select(x => new { x, a = b}).β

Filtering (MSDN)

Sorting (MSDN)

Inner Join + Grouping (MSDN)

Set Ops (MSDN) Zip zip operation defined by passed delegate

Concat (MSDN)

Aggregate Functions (MSDN)

Partitioning (MSDN).SkipWhile(x => x < 3)

Element Ops (MSDN) First = ignore rest Single = check single only, if not → error (exception)

Conversions (MSDN) + the rare magic: (let’s ignore these for now)