SlideShare a Scribd company logo
Mariano Sánchez – Software Architect
marianos@lagash.com
Local variable
type inference

var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

Query
expressions

Lambda
expressions

var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Extension
methods

Anonymous
types

Object
initializers
public class List<T>
{
public List<T> Where(Func<T, bool> predicate) { … }
public List<S> Select<S>(Func<T, S> selector) { … }
…
}
List<Customer> customers = GetCustomerList();
List<string> contacts =
customers.Where(c => c.State == "WA").Select(c => c.Name);

Que pasa con
otros tipos?

Que pasa con los que
ya implementan IList?
Que pasa con
los arrays?

Operadores de
query son métodos

Componiendo
métodos
Query como
metodos estáticos

public static class Sequence
{
public static IEnumerable<T> Where<T>(IEnumerable<T> source,
Func<T, bool> predicate) { … }

public static IEnumerable<S> Select<T, S>(IEnumerable<T> source,
Func<T, S> selector) { … }
…
}

Hmmm…
Customer[] customers = GetCustomerArray();
IEnumerable<string> contacts = Sequence.Select(
Sequence.Where(customers, c => c.State == "WA"),
c => c.Name);

Sería bueno en
IEnumerable<T>
namespace System.Query
Extension
{
methods
public static class Sequence
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> source,
Func<T, bool> predicate) { … }
public static IEnumerable<S> Select<T, S>(this IEnumerable<T> source,
Func<T, S> selector) { … }
…
}

Traigo las
extensiones

}
using System.Query;

IEnumerable<string> contacts =
customers.Where(c => c.State == "WA").Select(c => c.Name);

IntelliSense!

obj.Foo(x, y)

XXX.Foo(obj, x, y)
var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Extension
methods

Query
expressions
int i = 5;
string s = "Hello";
double d = 1.0;
int[] numbers = new int[] {1, 2, 3};
Dictionary<int,Order> orders = new Dictionary<int,Order>();
var i = 5;
var s = "Hello";
var d = 1.0;
var numbers = new int[] {1, 2, 3};
var orders = new Dictionary<int,Order>();
Local variable
type inference

var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
public class Point
{
private int x, y;

public int X { get { return x; } set { x = value; } }
public int Y { get { return y; } set { y = value; } }
}
Point a = new Point { X = 0, Y = 1 };

Point a = new Point();
a.X = 0;
a.Y = 1;

Asignar campos
o propiedades
Debe implementar
ICollection<T>
List<int> powers = new List<int>{ 1, 10, 100, 1000, 10000 };
List<int> powers = new List<int>();
powers.Add(1);
powers.Add(10);
powers.Add(100);
powers.Add(1000);
powers.Add(10000);
var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Object
initializers
public class Customer
{
public string Name;
public Address Address;
public string Phone;
public List<Order> Orders;
…
}

public class Contact
{
public string Name;
public string Phone;
}

Customer c = GetCustomer(…);
Contact x = new Contact { Name = c.Name, Phone = c.Phone };
Customer c = GetCustomer(…);
var x = new { Name = c.Name, Phone = c.Phone };

Customer c = GetCustomer(…);
var x = new { c.Name, c.Phone };
var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

var contacts =
customers.
.Where(c => c.State == "WA“)
.Select(c => new { c.Name, c.Phone });
foreach (var c in contacts) {
Console.WriteLine(c.Name);
Console.WriteLine(c.Phone);
}
var contacts =
from c in customers
where c.State == "WA"
select new { c.Name, c.Phone };

var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Anonymous
types
C# 3.0

VB 9.0

Others…

.NET Language Integrated Query

Standard
Query
Operators

Linq to Sql
(ADO.NET)

XLinq
(System.Xml)

<book>
<title/>
<author/>
<year/>
<price/>
</book>

Objects

SQL

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

catch
finally
try
catch
Restriction

Where, Contains

Projection

Select, SelectMany

Ordering

OrderBy, ThenBy

Grouping

GroupBy

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

Casting

OfType<T>
demo

LINQ to SQL
• Mejorar la forma de procesar Xml





Creando nuevas instancias
Modificando instancias existentes
Consultando instancias en memoria
Combinando consultas entre Xml, Objetos y Datos
<

>
<

</
<

</

</
>

>
<
<
<
<

</
<
>
>
<
<
<

</
<
>

>

/
type= home >
type= work >
>
<
>
<
>
<
> </
<
>
>
> </

>

>
</
</
</
</

>
>
>
>

>
</

>

>
</

type= mobile >
>
<
>
<
>
<
> </
>
<
>
</
>
> </
>

>
</
</
</
>

>
>

>
XmlDocument
XmlElement

new XmlDocument
"name"
“Diego Gonzalez"

XmlElement

"phone"
"type" "home"
"206-555-0144"

XmlElement

XmlElement
"contact"

"phone"
"type" "work"
"425-555-0145"

XmlElement
"street1"
"123 Main St"

XmlElement

XmlElement
"contacts"

"city"
"Mercer Island"

XmlElement

"state"
"WA"

XmlElement
"postal"
"68042"
XmlElement
"address"
XElement
new XElement "contacts"
new XElement "contact"
new XElement "name" "Patrick Hines"
new XElement "phone" "206-555-0144"
new XAttribute "type" "home"
new XElement "phone" "425-555-0145"
new XAttribute "type" "work"
new XElement "address"
new XElement "street1" "123 Main St"
new XElement "city" "Mercer Island"
new XElement "state" "WA"
new XElement "postal" "68042"
var result = new XElement "contacts"
from
in
"contact"
select new
new XComment "contact"
new XElement "name"
string
"name"
"phone"
new XElement "address"
"address"
• Basado en un modelo conceptual
 Entity y EntitySet

• Se mapea con el modelo relacional de la base de datos

permitiendo abarcar mas escenarios.
Muchas Gracias

Mariano Sánchez – Software Architect
marianos@lagash.com

More Related Content

What's hot

C++ Template
C++ TemplateC++ Template
C++ Template
Saket Pathak
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
Mahmoud Samir Fayed
 
Type script by Howard
Type script by HowardType script by Howard
Type script by Howard
LearningTech
 
Informatics Practices/ Information Practices Project (IP Project Class 12)
Informatics Practices/ Information Practices Project (IP Project Class 12)Informatics Practices/ Information Practices Project (IP Project Class 12)
Informatics Practices/ Information Practices Project (IP Project Class 12)
KushShah65
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by Howard
LearningTech
 
Data oriented design
Data oriented designData oriented design
Data oriented designMax Klyga
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
Muhammad khan
 
Templates
TemplatesTemplates
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 

What's hot (13)

C++ Template
C++ TemplateC++ Template
C++ Template
 
The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88The Ring programming language version 1.3 book - Part 24 of 88
The Ring programming language version 1.3 book - Part 24 of 88
 
Type script by Howard
Type script by HowardType script by Howard
Type script by Howard
 
Informatics Practices/ Information Practices Project (IP Project Class 12)
Informatics Practices/ Information Practices Project (IP Project Class 12)Informatics Practices/ Information Practices Project (IP Project Class 12)
Informatics Practices/ Information Practices Project (IP Project Class 12)
 
TypeScript by Howard
TypeScript by HowardTypeScript by Howard
TypeScript by Howard
 
Data oriented design
Data oriented designData oriented design
Data oriented design
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Template C++ OOP
Template C++ OOPTemplate C++ OOP
Template C++ OOP
 
Templates
TemplatesTemplates
Templates
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Final ds record
Final ds recordFinal ds record
Final ds record
 
Templates
TemplatesTemplates
Templates
 

Similar to Introducción a LINQ

Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01google
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
Shahriar Hyder
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
CHOOSE
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShakir Majeed Khan
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyasrsnarayanan
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
Vagif Abilov
 
PostThis
PostThisPostThis
PostThis
testingphase
 
Linq intro
Linq introLinq intro
Linq intro
Bình Trọng Án
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
Germán Küber
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
Shahriar Hyder
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
arihantmum
 
Dat402
Dat402Dat402
Dat402
ssa2010
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
ssa2010
 
Lesson11
Lesson11Lesson11
Lesson11
Alex Honcharuk
 
C# Starter L04-Collections
C# Starter L04-CollectionsC# Starter L04-Collections
C# Starter L04-CollectionsMohammad Shaker
 
Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Gyanendra Kumar
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
Sandeep Joshi
 

Similar to Introducción a LINQ (20)

Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01Linq 090701233237 Phpapp01
Linq 090701233237 Phpapp01
 
Introduction to Linq
Introduction to LinqIntroduction to Linq
Introduction to Linq
 
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
Ralf Laemmel - Not quite a sales pitch for C# 3.0 and .NET's LINQ - 2008-03-05
 
Share pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbsShare pointtechies linqtosp-andsbs
Share pointtechies linqtosp-andsbs
 
Linq Sanjay Vyas
Linq   Sanjay VyasLinq   Sanjay Vyas
Linq Sanjay Vyas
 
Greg Demo Slides
Greg Demo SlidesGreg Demo Slides
Greg Demo Slides
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
 
PostThis
PostThisPostThis
PostThis
 
Linq intro
Linq introLinq intro
Linq intro
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
help me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdfhelp me Java projectI put problem and my own code in the linkmy .pdf
help me Java projectI put problem and my own code in the linkmy .pdf
 
Dat402
Dat402Dat402
Dat402
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
 
Lesson11
Lesson11Lesson11
Lesson11
 
C# Starter L04-Collections
C# Starter L04-CollectionsC# Starter L04-Collections
C# Starter L04-Collections
 
SQL for ETL Testing
SQL for ETL TestingSQL for ETL Testing
SQL for ETL Testing
 
Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01Sqlforetltesting 130712042826-phpapp01
Sqlforetltesting 130712042826-phpapp01
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 

More from Mariano Sánchez

.NET Core
.NET Core.NET Core
.NET Core
Mariano Sánchez
 
ASP.NET Core 1.0
ASP.NET Core 1.0ASP.NET Core 1.0
ASP.NET Core 1.0
Mariano Sánchez
 
Introducción a SignalR
Introducción a SignalRIntroducción a SignalR
Introducción a SignalR
Mariano Sánchez
 
Visual Studio LightSwitch
Visual Studio LightSwitchVisual Studio LightSwitch
Visual Studio LightSwitch
Mariano Sánchez
 
HTML5 + Asp.NET
HTML5 + Asp.NETHTML5 + Asp.NET
HTML5 + Asp.NET
Mariano Sánchez
 
Hey Cortana!
Hey Cortana!Hey Cortana!
Hey Cortana!
Mariano Sánchez
 
Developing Universal Apps for Windows
Developing Universal Apps for WindowsDeveloping Universal Apps for Windows
Developing Universal Apps for Windows
Mariano Sánchez
 
Introducing the Windows Phone 8.1 App Development Platform
Introducing the Windows Phone 8.1 App Development PlatformIntroducing the Windows Phone 8.1 App Development Platform
Introducing the Windows Phone 8.1 App Development Platform
Mariano Sánchez
 
Visual Studio Online
Visual Studio OnlineVisual Studio Online
Visual Studio Online
Mariano Sánchez
 
Patrones Grasp
Patrones GraspPatrones Grasp
Patrones Grasp
Mariano Sánchez
 
Patrones de Diseño
Patrones de DiseñoPatrones de Diseño
Patrones de Diseño
Mariano Sánchez
 
Introducción a DDD
Introducción a DDDIntroducción a DDD
Introducción a DDD
Mariano Sánchez
 
Conociendo TypeScript
Conociendo TypeScriptConociendo TypeScript
Conociendo TypeScript
Mariano Sánchez
 
C# 6 - Que hay de nuevo?
C# 6 - Que hay de nuevo?C# 6 - Que hay de nuevo?
C# 6 - Que hay de nuevo?
Mariano Sánchez
 
Universal Windows Platform Programando para todos y todas
Universal Windows PlatformProgramando para todos y todasUniversal Windows PlatformProgramando para todos y todas
Universal Windows Platform Programando para todos y todas
Mariano Sánchez
 
Code Smells
Code SmellsCode Smells
Code Smells
Mariano Sánchez
 
Refactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existenteRefactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existente
Mariano Sánchez
 
Patrones de Diseño
Patrones de DiseñoPatrones de Diseño
Patrones de Diseño
Mariano Sánchez
 

More from Mariano Sánchez (18)

.NET Core
.NET Core.NET Core
.NET Core
 
ASP.NET Core 1.0
ASP.NET Core 1.0ASP.NET Core 1.0
ASP.NET Core 1.0
 
Introducción a SignalR
Introducción a SignalRIntroducción a SignalR
Introducción a SignalR
 
Visual Studio LightSwitch
Visual Studio LightSwitchVisual Studio LightSwitch
Visual Studio LightSwitch
 
HTML5 + Asp.NET
HTML5 + Asp.NETHTML5 + Asp.NET
HTML5 + Asp.NET
 
Hey Cortana!
Hey Cortana!Hey Cortana!
Hey Cortana!
 
Developing Universal Apps for Windows
Developing Universal Apps for WindowsDeveloping Universal Apps for Windows
Developing Universal Apps for Windows
 
Introducing the Windows Phone 8.1 App Development Platform
Introducing the Windows Phone 8.1 App Development PlatformIntroducing the Windows Phone 8.1 App Development Platform
Introducing the Windows Phone 8.1 App Development Platform
 
Visual Studio Online
Visual Studio OnlineVisual Studio Online
Visual Studio Online
 
Patrones Grasp
Patrones GraspPatrones Grasp
Patrones Grasp
 
Patrones de Diseño
Patrones de DiseñoPatrones de Diseño
Patrones de Diseño
 
Introducción a DDD
Introducción a DDDIntroducción a DDD
Introducción a DDD
 
Conociendo TypeScript
Conociendo TypeScriptConociendo TypeScript
Conociendo TypeScript
 
C# 6 - Que hay de nuevo?
C# 6 - Que hay de nuevo?C# 6 - Que hay de nuevo?
C# 6 - Que hay de nuevo?
 
Universal Windows Platform Programando para todos y todas
Universal Windows PlatformProgramando para todos y todasUniversal Windows PlatformProgramando para todos y todas
Universal Windows Platform Programando para todos y todas
 
Code Smells
Code SmellsCode Smells
Code Smells
 
Refactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existenteRefactoring - Mejorando el diseño del código existente
Refactoring - Mejorando el diseño del código existente
 
Patrones de Diseño
Patrones de DiseñoPatrones de Diseño
Patrones de Diseño
 

Recently uploaded

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 

Recently uploaded (20)

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 

Introducción a LINQ

  • 1. Mariano Sánchez – Software Architect marianos@lagash.com
  • 2.
  • 3.
  • 4.
  • 5.
  • 6. Local variable type inference var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; Query expressions Lambda expressions var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone }); Extension methods Anonymous types Object initializers
  • 7. public class List<T> { public List<T> Where(Func<T, bool> predicate) { … } public List<S> Select<S>(Func<T, S> selector) { … } … } List<Customer> customers = GetCustomerList(); List<string> contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); Que pasa con otros tipos? Que pasa con los que ya implementan IList? Que pasa con los arrays? Operadores de query son métodos Componiendo métodos
  • 8. Query como metodos estáticos public static class Sequence { public static IEnumerable<T> Where<T>(IEnumerable<T> source, Func<T, bool> predicate) { … } public static IEnumerable<S> Select<T, S>(IEnumerable<T> source, Func<T, S> selector) { … } … } Hmmm… Customer[] customers = GetCustomerArray(); IEnumerable<string> contacts = Sequence.Select( Sequence.Where(customers, c => c.State == "WA"), c => c.Name); Sería bueno en IEnumerable<T>
  • 9. namespace System.Query Extension { methods public static class Sequence { public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate) { … } public static IEnumerable<S> Select<T, S>(this IEnumerable<T> source, Func<T, S> selector) { … } … } Traigo las extensiones } using System.Query; IEnumerable<string> contacts = customers.Where(c => c.State == "WA").Select(c => c.Name); IntelliSense! obj.Foo(x, y)  XXX.Foo(obj, x, y)
  • 10. var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone }); Extension methods Query expressions
  • 11. int i = 5; string s = "Hello"; double d = 1.0; int[] numbers = new int[] {1, 2, 3}; Dictionary<int,Order> orders = new Dictionary<int,Order>(); var i = 5; var s = "Hello"; var d = 1.0; var numbers = new int[] {1, 2, 3}; var orders = new Dictionary<int,Order>();
  • 12. Local variable type inference var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone });
  • 13. public class Point { private int x, y; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } } Point a = new Point { X = 0, Y = 1 }; Point a = new Point(); a.X = 0; a.Y = 1; Asignar campos o propiedades
  • 14. Debe implementar ICollection<T> List<int> powers = new List<int>{ 1, 10, 100, 1000, 10000 }; List<int> powers = new List<int>(); powers.Add(1); powers.Add(10); powers.Add(100); powers.Add(1000); powers.Add(10000);
  • 15. var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone }); Object initializers
  • 16. public class Customer { public string Name; public Address Address; public string Phone; public List<Order> Orders; … } public class Contact { public string Name; public string Phone; } Customer c = GetCustomer(…); Contact x = new Contact { Name = c.Name, Phone = c.Phone }; Customer c = GetCustomer(…); var x = new { Name = c.Name, Phone = c.Phone }; Customer c = GetCustomer(…); var x = new { c.Name, c.Phone };
  • 17. var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers. .Where(c => c.State == "WA“) .Select(c => new { c.Name, c.Phone }); foreach (var c in contacts) { Console.WriteLine(c.Name); Console.WriteLine(c.Phone); }
  • 18. var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone }; var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone }); Anonymous types
  • 19. C# 3.0 VB 9.0 Others… .NET Language Integrated Query Standard Query Operators Linq to Sql (ADO.NET) XLinq (System.Xml) <book> <title/> <author/> <year/> <price/> </book> Objects SQL XML
  • 20. class Contact { … }; List<Contact> contacts = new List<Contacts>(); foreach(Customer c in customers) { if(c.State == “WA”) { Contact ct = new Contact(); ct.Name = c.Name; ct.Phone = c.Phone; contacts.Add(ct); } } var contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone };
  • 21.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28. Restriction Where, Contains Projection Select, SelectMany Ordering OrderBy, ThenBy Grouping GroupBy 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 Casting OfType<T>
  • 30.
  • 31. • Mejorar la forma de procesar Xml     Creando nuevas instancias Modificando instancias existentes Consultando instancias en memoria Combinando consultas entre Xml, Objetos y Datos
  • 32. < > < </ < </ </ > > < < < < </ < > > < < < </ < > > / type= home > type= work > > < > < > < > </ < > > > </ > > </ </ </ </ > > > > > </ > > </ type= mobile > > < > < > < > </ > < > </ > > </ > > </ </ </ > > > >
  • 33. XmlDocument XmlElement new XmlDocument "name" “Diego Gonzalez" XmlElement "phone" "type" "home" "206-555-0144" XmlElement XmlElement "contact" "phone" "type" "work" "425-555-0145" XmlElement "street1" "123 Main St" XmlElement XmlElement "contacts" "city" "Mercer Island" XmlElement "state" "WA" XmlElement "postal" "68042" XmlElement "address"
  • 34. XElement new XElement "contacts" new XElement "contact" new XElement "name" "Patrick Hines" new XElement "phone" "206-555-0144" new XAttribute "type" "home" new XElement "phone" "425-555-0145" new XAttribute "type" "work" new XElement "address" new XElement "street1" "123 Main St" new XElement "city" "Mercer Island" new XElement "state" "WA" new XElement "postal" "68042"
  • 35. var result = new XElement "contacts" from in "contact" select new new XComment "contact" new XElement "name" string "name" "phone" new XElement "address" "address"
  • 36.
  • 37. • Basado en un modelo conceptual  Entity y EntitySet • Se mapea con el modelo relacional de la base de datos permitiendo abarcar mas escenarios.
  • 38.
  • 39.
  • 40. Muchas Gracias Mariano Sánchez – Software Architect marianos@lagash.com