LINQ: Language Integrated Query. ST Colloquium, 2008-05-15 Tom Lokhorst

Similar documents
Seminar Datenbanksysteme

Visual C# 2012 Programming

TypeScript for C# developers. Making JavaScript manageable

: provid.ir

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

DBMS / Business Intelligence, SQL Server

HTSQL is a comprehensive navigational query language for relational databases.

Paging, sorting, and searching using EF Code first and MVC 3. Introduction. Installing AdventureWorksLT database. Creating the MVC 3 web application

TO HACK AN ASP.NET WEBSITE?

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

Databases 2011 The Relational Model and SQL

Financial Data Access with SQL, Excel & VBA

ADO.NET. Industrial Programming. Structure of database access. ADO.NET Example. Lecture 7: Database access in C# using LINQ

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Introduction to NoSQL Databases and MapReduce. Tore Risch Information Technology Uppsala University

Introduction to NoSQL Databases. Tore Risch Information Technology Uppsala University

A Brief Introduction to MySQL

Querying MongoDB without programming using FUNQL

Part I. Multiple Choice Questions (2 points each):

How to Design and Create Your Own Custom Ext Rep

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

SQL: Programming. Introduction to Databases CompSci 316 Fall 2014

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Intro to Web Programming. using PHP, HTTP, CSS, and Javascript Layton Smith CSE 4000

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

PHP Tutorial From beginner to master

Java EE Web Development Course Program

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Consuming and Producing Web Services with WST and JST. Christopher M. Judd. President/Consultant Judd Solutions, LLC

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

6. SQL/XML. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. 6.1 Introduction. XML Databases 6. SQL/XML. Creating XML documents from a database

database abstraction layer database abstraction layers in PHP Lukas Smith BackendMedia

PHP Language Binding Guide For The Connection Cloud Web Services

v1.1.0 SimpleSQL SQLite manager for Unity3D echo17.com

SAP Business Objects Business Intelligence platform Document Version: 4.1 Support Package Data Federation Administration Tool Guide

XML Databases 6. SQL/XML

ABSTRACT 1. INTRODUCTION. Kamil Bajda-Pawlikowski

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Programming Database lectures for mathema

FileMaker 12. ODBC and JDBC Guide

Real-time Streaming Analysis for Hadoop and Flume. Aaron Kimball odiago, inc. OSCON Data 2011

Social Relationship Analysis with Data Mining

Java SE 8 Programming

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

by Mario Fusco Monadic

SQL Server Array Library László Dobos, Alexander S. Szalay

Visual Basic Programming. An Introduction

Short notes on webpage programming languages

Weaving Stored Procedures into Java at Zalando

Big Data and Big Analytics

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Principles of Database Management Systems. Overview. Principles of Data Layout. Topic for today. "Executive Summary": here.

Database Design and Programming

Developing an ODBC C++ Client with MySQL Database

SQL injection: Not only AND 1=1. The OWASP Foundation. Bernardo Damele A. G. Penetration Tester Portcullis Computer Security Ltd

Oracle Database: SQL and PL/SQL Fundamentals

ORM2Pwn: Exploiting injections in Hibernate ORM

Database Query 1: SQL Basics

Jet Data Manager 2012 User Guide

How to make a good Software Requirement Specification(SRS)

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Setting Up a CLucene and PostgreSQL Federation

Excel Power Tools. David Onder and Alison Joseph. NCAIR 2015 Conference

Braindumps.C questions

To Java SE 8, and Beyond (Plan B)

CS346: Database Programming.

Maksym Iaroshenko Co-Founder and Senior Software Engineer at Eltrino. Magento non-mysql implementations

Moving from CS 61A Scheme to CS 61B Java

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

A collaborative platform for knowledge management

SQL/XML-IMDBg GPU boosted In-Memory Database for ultra fast data management Harald Frick CEO QuiLogic In-Memory DB Technology

WEB APPLICATION DEVELOPMENT. UNIT I J2EE Platform 9

Getting Started with Telerik Data Access. Contents

Instant SQL Programming

IVR Studio 3.0 Guide. May Knowlarity Product Team

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Bertrand Meyer. C#: Persistence

2015, André Melancia (Andy.PT) 1

Chapter 15 Functional Programming Languages

CS 378 Big Data Programming. Lecture 9 Complex Writable Types

Package sjdbc. R topics documented: February 20, 2015

Introduction to SQL for Data Scientists

NOSQL INTRODUCTION WITH MONGODB AND RUBY GEOFF

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Oracle Database: SQL and PL/SQL Fundamentals NEW

Efficient Interval Management in Microsoft SQL Server

Transcription:

LINQ: Language Integrated Query ST Colloquium, 2008-05-15 Tom Lokhorst

Brief example

Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42};

Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n;

Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n; foreach (var i in q) { Console.WriteLine(i); }

Brief example int[] nrs = {2, 3, 9, 1, 21, 3, 42}; var q = from n in nrs where n < 5 select n * n; LINQ foreach (var i in q) { Console.WriteLine(i); }

Overview The problem space LINQ in.net 3.5 Deconstructing expressions Usages of LINQ Comparisons with other stuff

Circles, Triangles and Rectangles

Circles, Triangles and Rectangles

Circles, Triangles and Rectangles Business software deals with lots of different types of data: Objects Tree structures (XML) Relational

Assignment A web server should show a list of the top 5 memory-intensive processes. Per process, show its name, and a description. There is a SQL database with descriptions for programs. The list should be in RSS format.

A bit of History

A bit of History.NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7.0 Delegates: managed function pointers

A bit of History.NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7.0 Delegates: managed function pointers.net 2.0 released late 2005 CLR 2.0, C# 2.0, VB 8.0 Generics, iterators, anonymous delegates

A bit of History.NET 1.0 released early 2002 CLR 1.0, C# 1.0, VB 7.0 Delegates: managed function pointers.net 2.0 released late 2005 CLR 2.0, C# 2.0, VB 8.0 Generics, iterators, anonymous delegates.net 3.5 released late 2007 CLR 2.0, C# 3.0, VB 9.0 Lambda expressions, local type inferencing, extension methods, LINQ

LINQ in.net 3.5 Libraries LINQ to Objects LINQ to XML LINQ to SQL Language enhancements Lambda expressions Query syntax Compiler enhancements Expression trees

LINQ to Objects

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 var q = from n in nrs where n < 5 select n * n;

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<int> q = from n in nrs where n < 5 select n * n;

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<int> q = nrs.where<int>(n => n < 5).Select<int, int>(n => n * n);

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<float> q = nrs.where<int>(n => n < 5).Select<int, float>(n => n * 0.5F);

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F));

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F)); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) {... }

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F)); public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) { foreach (var elem in source) if (predicate(elem)) yield return elem; }

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, n => n < 5), (n => n * 0.5F));

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 2.0 IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, delegate(int n){ return n < 5; }), delegate(int n){ return n * 0.5F;} );

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 1.0* IEnumerable<float> q = Enumerable.Select<int, float>( Enumerable.Where<int>(nrs, new Func<int, bool>(lessthanfive)), new Func<int, float>(timeshalf) ); bool LessThanFive(int n) { return n < 5; } float TimesHalf(int n) { return n * 0.5F; }

Eating the sugar int[] nrs = {2, 3, 9, 1, 21, 3, 42}; C# 3.0 var q = from n in nrs where n < 5 select n * n;

LINQ to Objects Query comprehenssions More syntax; orderby, groupby, join Works on you data structures IEnumerable<T> Implement you own Where<T>, Select<T> More library functions

LINQ to XML

Dealing with angle brackets LINQ to XML is a replacement for the W3C Document Object Model, because: It s old It s ugly It s impractical New API, all library: System.Xml.LINQ

Document Object Model Imperative API Document-centric Weird data types

Document Object Model Imperative API Document-centric Weird data types <Company name= Microsoft > <Founders> <Person>Bill Gates</Person> </Founders> </Company>

Document Object Model XmlDocument doc = new XmlDocument(); XmlElement company = doc.createelement("company"); doc.appendchild(company); XmlAttribute name = doc.createattribute("name"); name.value = "Microsoft"; company.attributes.append(name); XmlElement founders = doc.createelement("founders"); company.appendchild(founders); XmlElement person = doc.createelement("person"); founders.appendchild(person); XmlText bill = doc.createtextnode("bill Gates"); person.appendchild(bill);

LINQ to XML Functional construction Element-centric (context free) CLR data types (collection friendly)

LINQ to XML Functional construction Element-centric (context free) CLR data types (collection friendly) XElement company = new XElement("Company", new XAttribute("name", "Microsoft"), new XElement("Founders", new XElement("Person", "Bill Gates") ) );

XML with queries

XML with queries string[] names = { Anders, Erik, Amanda };

XML with queries string[] names = { Anders, Erik, Amanda }; from n in names where n.startswith( A ) select new XElement( Person, n)

XML with queries string[] names = { Anders, Erik, Amanda }; XElement employees = new XElement( Employees, from n in names where n.startswith( A ) select new XElement( Person, n) );

XML with queries string[] names = { Anders, Erik, Amanda }; XElement employees = new XElement( Employees, from n in names where n.startswith( A ) select new XElement( Person, n) ); company.add(employees);

XML with queries string[] names = { Anders, Erik, Amanda }; XElement employees = new XElement( Employees, from n in names where n.startswith( A ) select new XElement( Person, n) ); company.add(employees); Console.WriteLine(company);

XML on the Console <Company name= Microsoft > <Founders> <Person>Bill Gates</Person> </Founders> <Employees> <Person>Anders</Person> <Person>Amanda</Person> </Employees> </Company>

Querying XML IEnumerable<string> persons = from p in company.descendants( Person ) select p.value;

Querying XML IEnumerable<string> persons = from p in company.descendants( Person ) select p.value; Bill Gates Anders Amanda

LINQ to SQL

Querying a SQL backend Use LINQ to query a relational database Strongly typed queries Remote the query to the DBMS C# must remain API/DB independent

LINQ to SQL Simple Object Relational Mapper Microsoft SQL Server Comes with tool to make classes out of tables in an existing database Certainly not the best ORM out there More advanced stuff: LINQ to Entities

LINQ to SQL example

LINQ to SQL example

LINQ to SQL example

LINQ to SQL example [DatabaseAttribute(Name="Northwind")] public partial class NorthwindDataContext : System.Data.LINQ.DataContext { public NorthwindDataContext() : base("data Source=.;Initial Catalog=Northwind;" + "Integrated Security=True") { } } public Table<Customer> Customers { get { return this.gettable<customer>(); } }

LINQ to SQL example [Table(Name="dbo.Customers")] public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged { private string _CustomerID; [Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL", CanBeNull=false, IsPrimaryKey=true)] public string CustomerID { get { return this._customerid; } set { /* Setter code removed */ } } } /* More code be here */

LINQ to SQL example

LINQ to SQL example var db = new NorthwindDataContext(); var q = from c in db.customers where c.city == London select c;

LINQ to SQL example var db = new NorthwindDataContext(); var q = from c in db.customers where c.city == London select c; foreach (var c in q) Console.WriteLine(c.CustomerID + + c.contactname);

LINQ to SQL example var db = new NorthwindDataContext(); var q = from c in db.customers where c.city == London select c; foreach (var c in q) Console.WriteLine(c.CustomerID + + c.contactname); AROUT Thomas Hardy BSBEV Victoria Ashworth CONSH Elizabeth Brown EASTC Ann Devon NORTS Simon Crowther SEVES Hari Kumar

LINQ to SQL example var q = db.customers.where<customer>( c => c.city == London );

Lambdas again

Lambdas again Func<int, int> f = x => (x + 1) * 2; int nr = f(20); Console.WriteLine(nr); // Prints: 42

Lambdas again Expression<Func<int, int>> e = x => (x + 1) * 2;

Lambdas again Expression<Func<int, int>> e = x => (x + 1) * 2; string str = e.nodetype + + e.parameters.count; Console.WriteLine(str); // Prints: Lambda 1

Lambdas again Expression<Func<int, int>> e = x => (x + 1) * 2; string str = e.nodetype + + e.parameters.count; Console.WriteLine(str); // Prints: Lambda 1 Func<int, int> f = e.compile(); Console.WriteLine(f(20)); // Prints: 42

Lambdas again Expression<Func<int, int>> e = x => (x + 1) * 2;

Lambdas again Expression<Func<int, int>> e = x => (x + 1) * 2; ParameterExpression x = Expression.Parameter(typeof(int), "x"); Expression one = Expression.Constant(1, typeof(int)); Expression two = Expression.Constant(2, typeof(int)); Expression body = Expression.Multiply(Expression.Add(x, one), two); Expression<Func<int, int>> e = Expression.Lambda<Func<int, int>>(body, x);

LINQ to SQL example var q = db.customers.where<customer>( c => c.city == London );

LINQ to SQL example var q = db.customers.where<customer>( c => c.city == London ); ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); IQueryable<Customer> q = db.customers.where<customer>(expression.lambda<func<customer, bool>>( Expression.Equal( Expression.Property(c, (MethodInfo) methodof(customer.get_city)), Expression.Constant("London", typeof(string)), false, (MethodInfo) methodof(string.op_equality)), c));

LINQ to SQL example var q = db.customers.where<customer>( c => c.city == London ); SELECT C.* FROM Customers AS C WHERE C.City = London

Many more operations int x = db.customers.where<customer>( c => c.city == London ).Count(); SELECT COUNT(C.*) AS value FROM Customers AS C WHERE C.City = London

Many more operations int x = db.customers.where<customer>( c => c.city == London ).Take(4); SELECT TOP (4) C.* FROM Customers AS C WHERE C.City = London

Many more operations Restriction Projection Partitioning Join Concatinatin Ordering Grouping Set Conversion Elemement Generation Quantifiers Aggregate Where Select, SelectMany Take, Skip, TakeWhile, SkipWhile Join, GroupJoin Concat OrderBy, ThenBy, Reverse GroupBy Distinct, Union, Intersect, Except ToArray, ToList, ToDictionary, OfType, Cast First, FirstOrDefault, Last, LastOrDefault Range, Repeat, Empty Any, All, Contains Count, Sum, Min, Max, Average, Aggregate

Assignment

Assignment Solution

Assignment Solution new XElement("rss", new XAttribute("version", "2.0"), new XElement("channel", new XElement("title", "Top Processes"), from p in (from p in Process.GetProcesses() orderby p.virtualmemorysize descending select p).take(5) join pd in db.processdescriptions on p.processname equals pd.name into descrs from d in descrs.defaultifempty() select new XElement("item", new XElement("title", p.processname), d!= null? new XElement("description", d.description) : null) ) );

Assignment <rss version="2.0"> <channel> <title>top Processes</title> <item> <title>sqlservr</title> <description>sql Server</description> </item> <item> <title>sqlservr</title> <description>sql Server</description> </item> <item> <title>devenv</title> <description>visual Studio</description> </item> <item> <title>ssmsee</title> </item> <item> <title>reflector</title> </item> </channel> </rss> Solution

Other providers DbLinq (MySQL, PostgreSQL, Oracle) LINQ to Google LINQ to Entities Parallel LINQ DryadLINQ (Distributed execution engine)

Concluding... All LINQ is: Query comprehensions Libraries Expression trees Many more providers to come...

Questions?

Assignment in VB syntax Dim result = _ <rss version="2.0"> <channel>top Processes</channel> <%= From p In Process.GetProcesses() _ Order By p.virtualmemorysize Descending _ Take 5 _ Group Join pd In db.processdescriptions _ On p.processname Equals pd.name _ Into descrs = Group _ From d In descrs.defaultifempty _ Select <item> <title><%= p.processname %></title> <%= If(d IsNot Nothing, _ <description><%= d.description %></description>, _ Nothing) %> </item> %> </rss>

LINQ in Java // Setup a JPA entity manager... SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory(); EntityManagerFactory entitymanagerfactory = new EntityManagerFactoryImpl(sessionFactory, PersistenceUnitTransactionType.RESOURCE_LOCAL, true); QueryableEntityManager entitymanager = new QueryableEntityManager(entityManagerFactory.createEntityManager()); // Select all customers in the Washington region Iterable<Customer> wacustomers = from("c").in(entitymanager.entity(customer.class)). where(eq("c.getregion()", "WA")). select("c");

Monadic parser combinators using C# public abstract class MiniMLParsers<TInput> : CharParsers<TInput>{ public MiniMLParsers() { Whitespace = Rep(Char(' ').OR(Char('\t').OR(Char('\n')).OR(Char('\r')))); WsChr = chr => Whitespace.AND(Char(chr)); Id = from w in Whitespace from c in Char(char.IsLetter) from cs in Rep(Char(char.IsLetterOrDigit)) select cs.aggregate(c.tostring(),(acc,ch) => acc+ch); Ident = from s in Id where s!= "let" && s!= "in" select s; LetId = from s in Id where s == "let" select s; InId = from s in Id where s == "in" select s; Term1 = (from x in Ident select (Term)new VarTerm(x)).OR( (from u1 in WsChr('(') from t in Term from u2 in WsChr(')') select t)); Term = (from u1 in WsChr('\\') from x in Ident from u2 in WsChr('.') from t in Term

} from cs in Rep(Char(char.IsLetterOrDigit)) select cs.aggregate(c.tostring(),(acc,ch) => acc+ch); Ident = from s in Id where s!= "let" && s!= "in" select s; LetId = from s in Id where s == "let" select s; Monadic parser InId = from s in Id where s == "in" select s; Term1 = (from x in Ident select (Term)new VarTerm(x)) combinators using C#.OR( (from u1 in WsChr('(') from t in Term from u2 in WsChr(')') select t)); Term = (from u1 in WsChr('\\') from x in Ident from u2 in WsChr('.') from t in Term select (Term)new LambdaTerm(x,t)).OR( (from letid in LetId from x in Ident from u1 in WsChr('=') from t in Term from inid in InId from c in Term select (Term)new LetTerm(x,t,c))).OR( (from t in Term1 from ts in Rep(Term1) select (Term)new AppTerm(t,ts))); All = from t in Term from u in WsChr(';') select t;