SlideShare a Scribd company logo
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 have to construct SQL strings.
• PHP example:
if (some_condition()) {
$q = mysql_query(“select name from user were id = $id”)
...
}
• When will the problem be detected?
2
3
Searching in Collections
• Begin with a simple array of, say,
Customers.
Customer[] customers = new Customer[30];
customers[0] = new Customer(…);
…
customers[29] = new Customer(…);
4
Searching in Collections:
The Old Way
• Find the names of all London customers:
List<string> londoners = new List<string>();
foreach (Customer c in customers) {
if (c.City == “London”) {
londoners.add(c.Name);
}
}
5
Searching in Collections:
The LINQ Way
string[] londoners =
from c in customers
where c.City == “London”
select c.Name;
Declarative!
SQL-like!
No loops!
Returns a simple array!
Searching in Collections:
The LINQ Way
• LINQ is a C# feature
– Introduced in C# 3.0.
• LINQ = “Language INtegrated Query”
• So far, this is just list comprehension
added to C#.
• What did it take to add list
comprehension to the language?
7
LINQ: How Does It Work?
• LINQ syntax = shorthand for method
invocation.
• Syntactic sugar, using “Translation
maps”
8
Syntax Translation Example
string[] londoners =
from c in customers
where c.City == “London”
select c.Name;
string[] londoners =
customers.
Where(expression).
Select(expression);
9
Expressions == Methods?
• Where() wants a Boolean method.
• The method acts as a filter.
• Likewise for Select(): a translation
method.
10
Translating Expressions
• Problem: Translating
“c.City == “London””
to a boolean expression e, such that
Where(e) is valid?
11
C# Delegates
• C# delegates: method pointers.
• Since C# 1.0.
class Demo {
delegate void Foo();
void Bar() { … do something … };
void Test() {
Foo myDelegate = new Foo(Bar);
// “pointer” to Bar()
myDelegate(); // invoke
}
}
12
Delegates as Arguments
• Delegates can be passed as
arguments.
– Event handlers, jobs for threads, etc.
class Demo {
void Job() { … the job to carry out … };
void Test() {
Thread worker = new Thread(
new ThreadStart(Job));
worker.start();
}
}
13
Anonymous Methods
• Nameless methods = on-the-fly
delegates:
class Demo {
delegate void Foo();
void Test() {
Foo myDelegate = delegate() {
… do something …
};
myDelegate(); // invoke
}
}
14
Syntax Translation Example
string[] londoners =
from c in customers
where c.City == “London”
select c.Name;
string[] londoners =
customers.
Where(delegate(Customer c) {
return c.City == “London”; }).
Select(delegate(Customer c) {
return c.Name });
15
Well, Not Really.
•Where(), etc. accept delegate
methods.
• But LINQ creates lambda
expressions.
• Seamless conversion via
coercion.
16
Syntax Translation Example
string[] londoners =
from c in customers
where c.City == “London”
select c.Name;
string[] londoners =
customers.
Where(c => c.City == “London”).
Select(c => c.Name);
17
Lambda Expressions
• Lambda expression syntax:
(argumentList) => expression
oneArgument => expression
• Arguments optionally typed.
– Type inference mechanism.
– More on that later…
Shades of ML…
18
Where’s Where()?
• We invoked Where() on
Customers[].
• On the resulting Customers[], we
invoked Select().
• New methods for arrays!?
19
Extension Methods
class Utils {
public static firstChar(this string s)
{
return s.charAt(0);
}
}
• So far, just a simple static method.
• Can be used like any other.
20
Extension Methods
• But now…
Using Utils;
class Demo {
void Foo() {
string s = “Hello”;
Console.WriteLine(s.firstChar());
}
}
21
Extension Methods
• Static methods that seem to extend
existing types.
• Where(), Select(), etc. extend array
types in this manner.
22
Query Your Own Types!
• LINQ can be applied to any type,
not just built-in arrays and lists.
• Just implement Where(),
Select(), etc.
23
LINQ and Relational Data
• Let’s obtain a DB-table type, and query it.
DbCustomers customers = new DbCustomers(“my.mdb”);
string[] londoners =
from c in customers
where c.City == “London”
select c.Name;
24
This Makes No Sense!
• But… Where() applies the filter to
every record.
• … on the client!
• SELECT * FROM CUSTOMERS, and
filter with a simple loop!?
25
Back To Lambda
Expressions
• Lambda expressions can be
converted to anonymous methods.
• Can also be coerced to expression
trees.
– A run-time representation of the
syntax tree.
26
Example…
• Our code yields:
string[] londoners = customers.
Where(c => c.City == “London”).
Select(c => c.Name);
where “customers” is of type DbCustomers.
• No
DbCustomers.Where(delegate(Customer c))
method exists.
• However:
DbCustomers.Where(
Expression<Func<Customer,bool>> xt)
27
What Are Expression Trees?
• Any valid expression is converted by the
compiler to an expression tree.
– a.k.a. the abstract syntax tree of the expression.
– Normal part of the compilation process, in any language!
• Examples:
5 + 3 * 2 c.city == “London”
+
5 *
3 2
==
. (dot) “London”
c city
28
Expression Trees
• Normally, expression trees only exist at
compile-time.
• In C#, the compiler can create a run-time
representation of the expression tree.
– The language has a data type for expression trees.
– Represents lambda expressions at runtime.
• Used for generating SQL at runtime.
– Guaranteed to be syntactically valid, since it
was created from a valid C# expression.
It’s Just Coercion
• So, LINQ converts into expressions that
use Where(...), Select(...), etc.
• For some classes, Where(...) and
Select(...) accept delegates; for other
classes, they accept expression trees.
• Lambda expressions can be coerced
into either.
29
30
Projections
• Using LINQ’s select:
from c in customers
where c.City == “London”
select
new AddressBookEntry(c.Name, c.Phone);
31
Pre-Defined Types Only?
• But…
The projection type (e.g.,
AddressBookEntry) must be pre-
defined!
32
Ad-Hoc Types
• new { [name1 =] expr1,…, [ namen =] exprn}
• Type implied by types of exprs.
• Example:
from c in customers
where c.City == “London”
select new { c.Name, c.Phone };
If name is not specified, and
expr is either property or
x.property, then property’s
name will be used.
33
Ad-Hoc Types are Nameless
• How do we store the result?
??? q = from … select new {…};
• The ad-hoc type is nameless!
34
Auto-Typed Variables
• var x = 7; // x will be of type int
• var q = from … select new {…};
// q will be an array of the anonymous type
Console.WriteLine(q[0].Name);
• Local variables only.
– No auto-typing for fields or formal parameters.
Summary
• LINQ adds static SQL expression
correctness to C#.
• To do this, the following features were
added to C#:
– Lambda expressions.
– Extension methods.
– Expression types.
– List comprehension.
– Anonymous data types.
– Type inference.
35
There’s More
• There are several LINQ features we did
not present here, such as:
– Grouping (“GROUP BY” in SQL)
– Joins (selecting from multiple tables)
– ...
• These require even more language
features, such as closures.
36
What Is Happening to Languages?
• As new features are added to programming
languages, the languages evolve.
• Many of the features come from research or
experimental languages.
• Note how many of the new C# features
discussed here come from functional
languages like ML, Haskell or LISP:
– Lambda expressions, expression types, list
comprehension, anonymous data types, type
inference...
37
“Confessions of a Used Programming
Language Salesman”
• An 2007 essay by Eric Meijer (Microsoft).
• Discusses how features from functional
languages slowly creep into “mainstream”
languages.
• “Functional programming has finally reached
the masses, except that it is called Visual
Basic 9 instead of Haskell 98”.
38
A Glimpse Into the Future:
LISP (1958)
• We have seen the power of representing
program source at runtime (expression
trees).
• In LISP, program source can be
represented at runtime, but also generated
at runtime (or compile-time).
– Source code itself is a data structure (a list).
• LISP macros are light-years ahead of
C/C++ macros.
39
A Glimpse Into the Future:
LISP (1958)
• 50 years later, LISP features are slowly
re-appearing in mainstream languages.
– e.g., garbage collection, aspect-oriented
programming, and more.
• Conclusions:
– a. Learn from history.
– b. Know LISP, Haskell, etc: once you really
understand them, it will give you serious
advantages over ignorant software engineers
(even if you never use these languages in
practice).
40

More Related Content

What's hot

C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
sophoeutsen2
 
C++ to java
C++ to javaC++ to java
C++ to java
Ajmal Ak
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
Geeks Anonymes
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
xu liwei
 
Implicits Inspected and Explained @ ScalaDays 2016 Berlin
Implicits Inspected and Explained @ ScalaDays 2016 BerlinImplicits Inspected and Explained @ ScalaDays 2016 Berlin
Implicits Inspected and Explained @ ScalaDays 2016 Berlin
Tim Soethout
 
Implicits Inspected and Explained
Implicits Inspected and ExplainedImplicits Inspected and Explained
Implicits Inspected and Explained
Tim Soethout
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
Google Dart
Google DartGoogle Dart
Google Dart
Eberhard Wolff
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Kamal Acharya
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
JavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By SatyenJavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By Satyen
Satyen Pandya
 
Large Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScriptLarge Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScript
Oliver Zeigermann
 
ASP.NET
ASP.NETASP.NET
ASP.NET
chirag patil
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
NAVER Engineering
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
The Future of C# and Visual Basic
The Future of C# and Visual BasicThe Future of C# and Visual Basic
History of C#
History of C#History of C#
History of C#
aschlapsi
 
Creating Windows Runtime Components
Creating Windows Runtime Components Creating Windows Runtime Components
Creating Windows Runtime Components
Mirco Vanini
 
Применение паттерна Page Object для автоматизации веб сервисов - новый взгляд
Применение паттерна Page Object для автоматизации веб сервисов - новый взглядПрименение паттерна Page Object для автоматизации веб сервисов - новый взгляд
Применение паттерна Page Object для автоматизации веб сервисов - новый взгляд
COMAQA.BY
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
Bussines man badhrinadh
 

What's hot (20)

C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C++ to java
C++ to javaC++ to java
C++ to java
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Implicits Inspected and Explained @ ScalaDays 2016 Berlin
Implicits Inspected and Explained @ ScalaDays 2016 BerlinImplicits Inspected and Explained @ ScalaDays 2016 Berlin
Implicits Inspected and Explained @ ScalaDays 2016 Berlin
 
Implicits Inspected and Explained
Implicits Inspected and ExplainedImplicits Inspected and Explained
Implicits Inspected and Explained
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
 
Google Dart
Google DartGoogle Dart
Google Dart
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017Introduction to kotlin for android app development   gdg ahmedabad dev fest 2017
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
 
JavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By SatyenJavaScript – ECMAScript Basics By Satyen
JavaScript – ECMAScript Basics By Satyen
 
Large Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScriptLarge Scale JavaScript with TypeScript
Large Scale JavaScript with TypeScript
 
ASP.NET
ASP.NETASP.NET
ASP.NET
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
The Future of C# and Visual Basic
The Future of C# and Visual BasicThe Future of C# and Visual Basic
The Future of C# and Visual Basic
 
History of C#
History of C#History of C#
History of C#
 
Creating Windows Runtime Components
Creating Windows Runtime Components Creating Windows Runtime Components
Creating Windows Runtime Components
 
Применение паттерна Page Object для автоматизации веб сервисов - новый взгляд
Применение паттерна Page Object для автоматизации веб сервисов - новый взглядПрименение паттерна Page Object для автоматизации веб сервисов - новый взгляд
Применение паттерна Page Object для автоматизации веб сервисов - новый взгляд
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 

Similar to linq with Exampls

LINQ.ppt
LINQ.pptLINQ.ppt
Think in linq
Think in linqThink in linq
Think in linq
Sudipta Mukherjee
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
ANURAG SINGH
 
Return of c++
Return of c++Return of c++
Return of c++
Yongwei Wu
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Kathmandu University
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
SURBHI SAROHA
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)
Christian Nagel
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
Stephen Colebourne
 
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
Muralidharan Deenathayalan
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
Paul Irwin
 
Exciting JavaScript - Part II
Exciting JavaScript - Part IIExciting JavaScript - Part II
Exciting JavaScript - Part II
Eugene Lazutkin
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
Mohsen Zainalpour
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
temkin abdlkader
 
C#unit4
C#unit4C#unit4
C#unit4
raksharao
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
AboutYouGmbH
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbs
Shakir Majeed Khan
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
Fisnik Doko
 
Litwin linq
Litwin linqLitwin linq
Litwin linq
Jitendra Gangwar
 
Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >
Sergey Platonov
 
Link quries
Link quriesLink quries
Link quries
ulfat mushtaq
 

Similar to linq with Exampls (20)

LINQ.ppt
LINQ.pptLINQ.ppt
LINQ.ppt
 
Think in linq
Think in linqThink in linq
Think in linq
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Return of c++
Return of c++Return of c++
Return of c++
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 
C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)C# What's next? (7.x and 8.0)
C# What's next? (7.x and 8.0)
 
Java SE 8 library design
Java SE 8 library designJava SE 8 library design
Java SE 8 library design
 
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
 
Exciting JavaScript - Part II
Exciting JavaScript - Part IIExciting JavaScript - Part II
Exciting JavaScript - Part II
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
C#unit4
C#unit4C#unit4
C#unit4
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbs
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
Litwin linq
Litwin linqLitwin linq
Litwin linq
 
Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >Антон Бикинеев, Writing good std::future&lt; C++ >
Антон Бикинеев, Writing good std::future&lt; C++ >
 
Link quries
Link quriesLink quries
Link quries
 

Recently uploaded

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 

Recently uploaded (20)

HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 

linq with Exampls

  • 1. 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 have to construct SQL strings. • PHP example: if (some_condition()) { $q = mysql_query(“select name from user were id = $id”) ... } • When will the problem be detected? 2
  • 2. 3 Searching in Collections • Begin with a simple array of, say, Customers. Customer[] customers = new Customer[30]; customers[0] = new Customer(…); … customers[29] = new Customer(…); 4 Searching in Collections: The Old Way • Find the names of all London customers: List<string> londoners = new List<string>(); foreach (Customer c in customers) { if (c.City == “London”) { londoners.add(c.Name); } }
  • 3. 5 Searching in Collections: The LINQ Way string[] londoners = from c in customers where c.City == “London” select c.Name; Declarative! SQL-like! No loops! Returns a simple array! Searching in Collections: The LINQ Way • LINQ is a C# feature – Introduced in C# 3.0. • LINQ = “Language INtegrated Query” • So far, this is just list comprehension added to C#. • What did it take to add list comprehension to the language?
  • 4. 7 LINQ: How Does It Work? • LINQ syntax = shorthand for method invocation. • Syntactic sugar, using “Translation maps” 8 Syntax Translation Example string[] londoners = from c in customers where c.City == “London” select c.Name; string[] londoners = customers. Where(expression). Select(expression);
  • 5. 9 Expressions == Methods? • Where() wants a Boolean method. • The method acts as a filter. • Likewise for Select(): a translation method. 10 Translating Expressions • Problem: Translating “c.City == “London”” to a boolean expression e, such that Where(e) is valid?
  • 6. 11 C# Delegates • C# delegates: method pointers. • Since C# 1.0. class Demo { delegate void Foo(); void Bar() { … do something … }; void Test() { Foo myDelegate = new Foo(Bar); // “pointer” to Bar() myDelegate(); // invoke } } 12 Delegates as Arguments • Delegates can be passed as arguments. – Event handlers, jobs for threads, etc. class Demo { void Job() { … the job to carry out … }; void Test() { Thread worker = new Thread( new ThreadStart(Job)); worker.start(); } }
  • 7. 13 Anonymous Methods • Nameless methods = on-the-fly delegates: class Demo { delegate void Foo(); void Test() { Foo myDelegate = delegate() { … do something … }; myDelegate(); // invoke } } 14 Syntax Translation Example string[] londoners = from c in customers where c.City == “London” select c.Name; string[] londoners = customers. Where(delegate(Customer c) { return c.City == “London”; }). Select(delegate(Customer c) { return c.Name });
  • 8. 15 Well, Not Really. •Where(), etc. accept delegate methods. • But LINQ creates lambda expressions. • Seamless conversion via coercion. 16 Syntax Translation Example string[] londoners = from c in customers where c.City == “London” select c.Name; string[] londoners = customers. Where(c => c.City == “London”). Select(c => c.Name);
  • 9. 17 Lambda Expressions • Lambda expression syntax: (argumentList) => expression oneArgument => expression • Arguments optionally typed. – Type inference mechanism. – More on that later… Shades of ML… 18 Where’s Where()? • We invoked Where() on Customers[]. • On the resulting Customers[], we invoked Select(). • New methods for arrays!?
  • 10. 19 Extension Methods class Utils { public static firstChar(this string s) { return s.charAt(0); } } • So far, just a simple static method. • Can be used like any other. 20 Extension Methods • But now… Using Utils; class Demo { void Foo() { string s = “Hello”; Console.WriteLine(s.firstChar()); } }
  • 11. 21 Extension Methods • Static methods that seem to extend existing types. • Where(), Select(), etc. extend array types in this manner. 22 Query Your Own Types! • LINQ can be applied to any type, not just built-in arrays and lists. • Just implement Where(), Select(), etc.
  • 12. 23 LINQ and Relational Data • Let’s obtain a DB-table type, and query it. DbCustomers customers = new DbCustomers(“my.mdb”); string[] londoners = from c in customers where c.City == “London” select c.Name; 24 This Makes No Sense! • But… Where() applies the filter to every record. • … on the client! • SELECT * FROM CUSTOMERS, and filter with a simple loop!?
  • 13. 25 Back To Lambda Expressions • Lambda expressions can be converted to anonymous methods. • Can also be coerced to expression trees. – A run-time representation of the syntax tree. 26 Example… • Our code yields: string[] londoners = customers. Where(c => c.City == “London”). Select(c => c.Name); where “customers” is of type DbCustomers. • No DbCustomers.Where(delegate(Customer c)) method exists. • However: DbCustomers.Where( Expression<Func<Customer,bool>> xt)
  • 14. 27 What Are Expression Trees? • Any valid expression is converted by the compiler to an expression tree. – a.k.a. the abstract syntax tree of the expression. – Normal part of the compilation process, in any language! • Examples: 5 + 3 * 2 c.city == “London” + 5 * 3 2 == . (dot) “London” c city 28 Expression Trees • Normally, expression trees only exist at compile-time. • In C#, the compiler can create a run-time representation of the expression tree. – The language has a data type for expression trees. – Represents lambda expressions at runtime. • Used for generating SQL at runtime. – Guaranteed to be syntactically valid, since it was created from a valid C# expression.
  • 15. It’s Just Coercion • So, LINQ converts into expressions that use Where(...), Select(...), etc. • For some classes, Where(...) and Select(...) accept delegates; for other classes, they accept expression trees. • Lambda expressions can be coerced into either. 29 30 Projections • Using LINQ’s select: from c in customers where c.City == “London” select new AddressBookEntry(c.Name, c.Phone);
  • 16. 31 Pre-Defined Types Only? • But… The projection type (e.g., AddressBookEntry) must be pre- defined! 32 Ad-Hoc Types • new { [name1 =] expr1,…, [ namen =] exprn} • Type implied by types of exprs. • Example: from c in customers where c.City == “London” select new { c.Name, c.Phone }; If name is not specified, and expr is either property or x.property, then property’s name will be used.
  • 17. 33 Ad-Hoc Types are Nameless • How do we store the result? ??? q = from … select new {…}; • The ad-hoc type is nameless! 34 Auto-Typed Variables • var x = 7; // x will be of type int • var q = from … select new {…}; // q will be an array of the anonymous type Console.WriteLine(q[0].Name); • Local variables only. – No auto-typing for fields or formal parameters.
  • 18. Summary • LINQ adds static SQL expression correctness to C#. • To do this, the following features were added to C#: – Lambda expressions. – Extension methods. – Expression types. – List comprehension. – Anonymous data types. – Type inference. 35 There’s More • There are several LINQ features we did not present here, such as: – Grouping (“GROUP BY” in SQL) – Joins (selecting from multiple tables) – ... • These require even more language features, such as closures. 36
  • 19. What Is Happening to Languages? • As new features are added to programming languages, the languages evolve. • Many of the features come from research or experimental languages. • Note how many of the new C# features discussed here come from functional languages like ML, Haskell or LISP: – Lambda expressions, expression types, list comprehension, anonymous data types, type inference... 37 “Confessions of a Used Programming Language Salesman” • An 2007 essay by Eric Meijer (Microsoft). • Discusses how features from functional languages slowly creep into “mainstream” languages. • “Functional programming has finally reached the masses, except that it is called Visual Basic 9 instead of Haskell 98”. 38
  • 20. A Glimpse Into the Future: LISP (1958) • We have seen the power of representing program source at runtime (expression trees). • In LISP, program source can be represented at runtime, but also generated at runtime (or compile-time). – Source code itself is a data structure (a list). • LISP macros are light-years ahead of C/C++ macros. 39 A Glimpse Into the Future: LISP (1958) • 50 years later, LISP features are slowly re-appearing in mainstream languages. – e.g., garbage collection, aspect-oriented programming, and more. • Conclusions: – a. Learn from history. – b. Know LISP, Haskell, etc: once you really understand them, it will give you serious advantages over ignorant software engineers (even if you never use these languages in practice). 40