SlideShare a Scribd company logo
Mohammad Shaker
mohammadshaker.com
C# Programming Course
@ZGTRShaker
2011, 2012, 2013, 2014
C# Starter
L05 – LINQ and Lambda Expressions
LINQ
LINQ
One of the most awesome things ever done!
LINQ
Where’s Java?
LINQ
Where’s Java?
2013 with some tool (not as good as .NET)
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
List<string> queryResults = new List<string>();
foreach (var item in names)
{
if (n.StartsWith(“S”))
{
queryResults.Add(item);
}
}
var queryResults = from n in names
where n.StartsWith("S")
select n;
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
List<string> queryResults = new List<string>();
foreach (var item in names)
{
if (n.StartsWith(“S”))
{
queryResults.Add(item);
}
}
var queryResults = from n in names
where n.StartsWith("S")
select n;
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
List<string> queryResults = new List<string>();
foreach (var item in names)
{
if (n.StartsWith(“S”))
{
queryResults.Add(item);
}
}
var queryResults = from n in names
where n.StartsWith("S")
select n;
The SAME!
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
List<string> queryResults = new List<string>();
foreach (var item in names)
{
if (n.StartsWith(“S”))
{
queryResults.Add(item);
}
}
var queryResults = from n in names
where n.StartsWith("S")
select n;
The SAME!
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Smith
Smythe
Small
Singh
Samba
Program finished, press Enter/Return to continue:
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Alonso
Zheng
Smith
Jones
Smythe
Small
Ruiz
Hsieh
Jorgenson
Ilyich
Singh
Samba
Program finished, press Enter/Return to continue:
LINQ
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Alonso
Zheng
Smith
Jones
Smythe
Small
Ruiz
Hsieh
Jorgenson
Ilyich
Singh
Samba
Program finished, press Enter/Return to continue:
Lambda Expressions
Tons of code in a line
Lambda Expressions
• The operator => is called the lambda operator.
n => n < 1000
“ Lambda ” comes from the lambda calculus, the mathematical formalism underlying
functional programming languages, which are the kind of programming on which LINQ is
based. If you are interested, check out sources such as the Wikipedia article on lambda
calculus. You don ’ t need to understand the mathematics to use lambda functions, although
understanding functional programming is helpful for advanced LINQ programming.
Lambda Expressions
var queryResults = from n in names
where n.StartsWith("S")
select n;
Lambda Expressions
n => n.StartsWith(“S”)
var queryResults = from n in names
where n.StartsWith("S")
select n;
Lambda Expressions
n => n.StartsWith(“S”)
var queryResults = from n in names
where n.StartsWith("S")
select n;
Lambda Expressions
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Lambda Expressions
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Lambda Expressions
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Smith
Smythe
Small
Singh
Samba
Program finished, press Enter/Return to continue:
Query Techniques
Ordering Query Results
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
orderby n
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Ordering Query Results
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = from n in names
where n.StartsWith("S")
orderby n
select n;
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Samba
Singh
Small
Smith
Smythe
Program finished, press Enter/Return to continue:
Ordering Query Results
• Descending order
• This orders the example results as follows:
orderby n descending
Smythe
Smith
Small
Singh
Samba
Ordering Query Results
• Ordering Using Method Syntax
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”));
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Ordering Query Results
• Ordering Using Method Syntax
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”));
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Samba
Singh
Small
Smith
Smythe
Program finished, press Enter/Return to continue:
Ordering Query Results
• Ordering Using Method Syntax
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”));
static void Main(string[] args)
{
string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe",
"Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"};
var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S"));
Console.WriteLine("Names beginning with S:");
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Console.Write("Program finished, press Enter/Return to continue:");
Console.ReadLine();
}
Names beginning with S:
Samba
Singh
Small
Smith
Smythe
Program finished, press Enter/Return to continue:
IOrderedEnumerable < string > and IEnumerable < string >
Ordering Query Results
• Descending order
var queryResults = names.OrderByDescending(n => n).Where(n => n.StartsWith(“S”));
Ordering Query Results
• What does this do?
var queryResults =
names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
Ordering Query Results
• What does this do?
var queryResults =
names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
Ordering Query Results
• What does this do?
var queryResults =
names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
Querying Complex Objects
Querying Complex Objects
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
}
Querying Complex Objects
static void Main(string[] args)
{
List < Customer > customers = new List < Customer >
{
new Customer { ID=”A”, City=”New York”, Country=”USA”,Region=”North America”, Sales=9999},
new Customer { ID=”B”, City=”Mumbai”, Country=”India”,Region=”Asia”, Sales=8888 },
new Customer { ID=”C”, City=”Karachi”, Country=”Pakistan”,Region=”Asia”, Sales=7777 },
new Customer { ID=”D”, City=”Delhi”, Country=”India”,Region=”Asia”, Sales=6666 },
new Customer { ID=”E”, City=”S ã o Paulo”, Country=”Brazil”,Region=”South America”, Sales=5555 },
new Customer { ID=”F”, City=”Moscow”, Country=”Russia”,Region=”Europe”, Sales=4444 },
…
};
var queryResults =
from c in customers
where c.Region == “Asia”
select c;
Console.WriteLine(“Customers in Asia:”);
foreach (Customer c in queryResults)
{
Console.WriteLine(c);
}
Console.Write(“Program finished, press Enter/Return to continue:”);
Console.ReadLine();
}
Querying Complex Objects
• The output:
• Why?!
– Because ToString() is called for each Customer object. ToString() Is inherited directly
from the object class.
• Solution: Override the ToString() method.
Customers in Asia:
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
CSharpCourse2011.Customer
Program finished, press Enter/Return to continue:
Querying Complex Objects
• From this:
• To this:
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
public override string ToString()
{
return “ID: “ + ID + “ City: “ + City + “ Country: “ + Country +
“ Region: “ + Region + “ Sales: “ + Sales;
}
}
class Customer
{
public string ID { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Region { get; set; }
public decimal Sales { get; set; }
}
Querying Complex Objects
• The output now is:
Customers in Asia:
ID: B City: Mumbai Country: India Region: Asia Sales: 8888
ID: C City: Karachi Country: Pakistan Region: Asia Sales: 7777
ID: D City: Delhi Country: India Region: Asia Sales: 6666
ID: G City: Seoul Country: Korea Region: Asia Sales: 3333
ID: H City: Istanbul Country: Turkey Region: Asia Sales: 2222
ID: I City: Shanghai Country: China Region: Asia Sales: 1111
ID: L City: Jakarta Country: Indonesia Region: Asia Sales: 3000
ID: M City: Tokyo Country: Japan Region: Asia Sales: 4000
ID: P City: Tehran Country: Iran Region: Asia Sales: 7000
ID: R City: Beijing Country: China Region: Asia Sales: 9000
Program finished, press Enter/Return to continue:
Querying Complex Objects
var queryResults =
from c in customers
where c.Region == “Asia”
select c.City;
Mumbai
Karachi
Delhi
Seoul
Istanbul
Shanghai
Jakarta
Tokyo
Tehran
Beijing
Selecting Multiple Properties
Querying Complex Objects
• Let’s see this:
select c.City, c.Country, c.Sales
Querying Complex Objects
• Let’s see this:
• It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause!
– So, what’s the solution?
• Just creating a new object on-the-fly in the select clause to hold the results you want for your query!
select c.City, c.Country, c.Sales
Querying Complex Objects
• Let’s see this:
• It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause!
– So, what’s the solution?
• Just creating a new object on-the-fly in the select clause to hold the results you want for your query!
select c.City, c.Country, c.Sales
var queryResults =
from c in customers
where c.Region == “North America”
select new { c.City, c.Country, c.Sales };
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Querying Complex Objects
• Let’s see this:
• It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause!
– So, what’s the solution?
• Just creating a new object on-the-fly in the select clause to hold the results you want for your query!
select c.City, c.Country, c.Sales
var queryResults =
from c in customers
where c.Region == “North America”
select new { c.City, c.Country, c.Sales };
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
{ City = New York, Country = USA, Sales = 9999 }
{ City = Mexico City, Country = Mexico, Sales = 2000 }
{ City = Los Angeles, Country = USA, Sales = 5000 }
Program finished, press Enter/Return to continue:
Querying Complex Objects
• Now, instead of all this:
• Let’s try this:
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
var queryResults =
from c in customers
where c.Region == “North America”
select new { c.City, c.Country, c.Sales };
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Querying Complex Objects
• Now, instead of all this:
• Let’s try this:
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
{ City = New York, Country = USA, Sales = 9999 }
{ City = Mexico City, Country = Mexico, Sales = 2000 }
{ City = Los Angeles, Country = USA, Sales = 5000 }
Program finished, press Enter/Return to continue:
var queryResults =
from c in customers
where c.Region == “North America”
select new { c.City, c.Country, c.Sales };
foreach (var item in queryResults)
{
Console.WriteLine(item);
}
Querying Complex Objects
• Now, instead of this:
• Let’s try this:
var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales })
.Where(c => c.Region == “North America”);
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
Querying Complex Objects
• Now, instead of this:
• Let’s try this:
• It’s a Compiler Error! Because: The Region property is not included in the
anonymous type.
{c.City, c.Country, c.Sales } created by the Select() projection. The
compiler doesn’t know it yet!
var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales })
.Where(c => c.Region == “North America”);
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
Querying Complex Objects
• OK, now, instead of this:
• Let’s try this:
var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales })
.Where(c => c.City == “New York”);
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
Querying Complex Objects
• OK, now, instead of this:
• Let’s try this:
• Works beautifully!
var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales })
.Where(c => c.City == “New York”);
var queryResults = customers.Where(c => c.Region == “North America”)
.Select(c => new { c.City, c.Country, c.Sales });
Querying Complex Objects
• Distinct
var queryResults = customers.Select(c => c.Region).Distinct();
var queryResults = (from c in customers select c.Region).Distinct();
Ordering By Multiple Levels
Ordering By Multiple Levels
var queryResults =
from c in customers
orderby c.Region, c.Country, c.City
select new { c.ID, c.Region, c.Country, c.City };
Ordering By Multiple Levels
var queryResults =
from c in customers
orderby c.Region, c.Country, c.City
select new { c.ID, c.Region, c.Country, c.City };
{ ID = O, Region = Africa, Country = Egypt, City = Cairo }
{ ID = J, Region = Africa, Country = Nigeria, City = Lagos }
{ ID = R, Region = Asia, Country = China, City = Beijing }
{ ID = I, Region = Asia, Country = China, City = Shanghai }
{ ID = D, Region = Asia, Country = India, City = Delhi }
{ ID = B, Region = Asia, Country = India, City = Mumbai }
{ ID = L, Region = Asia, Country = Indonesia, City = Jakarta }
{ ID = P, Region = Asia, Country = Iran, City = Tehran }
{ ID = M, Region = Asia, Country = Japan, City = Tokyo }
{ ID = G, Region = Asia, Country = Korea, City = Seoul }
….
Drilling further
• Many other techniques
• Grouping
• Joining
• Take
• Skip
• ….

More Related Content

More from Mohammad Shaker

Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
Mohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
Mohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
Mohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
Mohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
Mohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
Mohammad Shaker
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
Mohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
Mohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
Mohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
Mohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
Mohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
Mohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
Mohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
Mohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
Mohammad Shaker
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
Mohammad Shaker
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
Mohammad Shaker
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
Mohammad Shaker
 

More from Mohammad Shaker (20)

Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 
Indie Series 03: Becoming an Indie
Indie Series 03: Becoming an IndieIndie Series 03: Becoming an Indie
Indie Series 03: Becoming an Indie
 
Indie Series 01: Intro to Games
Indie Series 01: Intro to GamesIndie Series 01: Intro to Games
Indie Series 01: Intro to Games
 

Recently uploaded

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 

Recently uploaded (20)

To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 

C# Starter L05-LINQ

  • 1. Mohammad Shaker mohammadshaker.com C# Programming Course @ZGTRShaker 2011, 2012, 2013, 2014 C# Starter L05 – LINQ and Lambda Expressions
  • 3. LINQ One of the most awesome things ever done!
  • 5. LINQ Where’s Java? 2013 with some tool (not as good as .NET)
  • 6. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 7. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 8. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } List<string> queryResults = new List<string>(); foreach (var item in names) { if (n.StartsWith(“S”)) { queryResults.Add(item); } } var queryResults = from n in names where n.StartsWith("S") select n;
  • 9. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } List<string> queryResults = new List<string>(); foreach (var item in names) { if (n.StartsWith(“S”)) { queryResults.Add(item); } } var queryResults = from n in names where n.StartsWith("S") select n;
  • 10. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } List<string> queryResults = new List<string>(); foreach (var item in names) { if (n.StartsWith(“S”)) { queryResults.Add(item); } } var queryResults = from n in names where n.StartsWith("S") select n; The SAME!
  • 11. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } List<string> queryResults = new List<string>(); foreach (var item in names) { if (n.StartsWith(“S”)) { queryResults.Add(item); } } var queryResults = from n in names where n.StartsWith("S") select n; The SAME!
  • 12. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 13. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Smith Smythe Small Singh Samba Program finished, press Enter/Return to continue:
  • 14. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 15. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Alonso Zheng Smith Jones Smythe Small Ruiz Hsieh Jorgenson Ilyich Singh Samba Program finished, press Enter/Return to continue:
  • 16. LINQ static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Alonso Zheng Smith Jones Smythe Small Ruiz Hsieh Jorgenson Ilyich Singh Samba Program finished, press Enter/Return to continue:
  • 18. Tons of code in a line
  • 19. Lambda Expressions • The operator => is called the lambda operator. n => n < 1000 “ Lambda ” comes from the lambda calculus, the mathematical formalism underlying functional programming languages, which are the kind of programming on which LINQ is based. If you are interested, check out sources such as the Wikipedia article on lambda calculus. You don ’ t need to understand the mathematics to use lambda functions, although understanding functional programming is helpful for advanced LINQ programming.
  • 20. Lambda Expressions var queryResults = from n in names where n.StartsWith("S") select n;
  • 21. Lambda Expressions n => n.StartsWith(“S”) var queryResults = from n in names where n.StartsWith("S") select n;
  • 22. Lambda Expressions n => n.StartsWith(“S”) var queryResults = from n in names where n.StartsWith("S") select n;
  • 23. Lambda Expressions static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 24. Lambda Expressions static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 25. Lambda Expressions static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Smith Smythe Small Singh Samba Program finished, press Enter/Return to continue:
  • 27. Ordering Query Results static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") orderby n select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 28. Ordering Query Results static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = from n in names where n.StartsWith("S") orderby n select n; Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Samba Singh Small Smith Smythe Program finished, press Enter/Return to continue:
  • 29. Ordering Query Results • Descending order • This orders the example results as follows: orderby n descending Smythe Smith Small Singh Samba
  • 30. Ordering Query Results • Ordering Using Method Syntax var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”)); static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }
  • 31. Ordering Query Results • Ordering Using Method Syntax var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”)); static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Samba Singh Small Smith Smythe Program finished, press Enter/Return to continue:
  • 32. Ordering Query Results • Ordering Using Method Syntax var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith(“S”)); static void Main(string[] args) { string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba"}; var queryResults = names.OrderBy(n => n).Where(n => n.StartsWith("S")); Console.WriteLine("Names beginning with S:"); foreach (var item in queryResults) { Console.WriteLine(item); } Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); } Names beginning with S: Samba Singh Small Smith Smythe Program finished, press Enter/Return to continue: IOrderedEnumerable < string > and IEnumerable < string >
  • 33. Ordering Query Results • Descending order var queryResults = names.OrderByDescending(n => n).Where(n => n.StartsWith(“S”));
  • 34. Ordering Query Results • What does this do? var queryResults = names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
  • 35. Ordering Query Results • What does this do? var queryResults = names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
  • 36. Ordering Query Results • What does this do? var queryResults = names.OrderBy(n => n.Substring(n.Length-1)).Where(n => n.StartsWith(“S”));
  • 38. Querying Complex Objects class Customer { public string ID { get; set; } public string City { get; set; } public string Country { get; set; } public string Region { get; set; } public decimal Sales { get; set; } }
  • 39. Querying Complex Objects static void Main(string[] args) { List < Customer > customers = new List < Customer > { new Customer { ID=”A”, City=”New York”, Country=”USA”,Region=”North America”, Sales=9999}, new Customer { ID=”B”, City=”Mumbai”, Country=”India”,Region=”Asia”, Sales=8888 }, new Customer { ID=”C”, City=”Karachi”, Country=”Pakistan”,Region=”Asia”, Sales=7777 }, new Customer { ID=”D”, City=”Delhi”, Country=”India”,Region=”Asia”, Sales=6666 }, new Customer { ID=”E”, City=”S ã o Paulo”, Country=”Brazil”,Region=”South America”, Sales=5555 }, new Customer { ID=”F”, City=”Moscow”, Country=”Russia”,Region=”Europe”, Sales=4444 }, … }; var queryResults = from c in customers where c.Region == “Asia” select c; Console.WriteLine(“Customers in Asia:”); foreach (Customer c in queryResults) { Console.WriteLine(c); } Console.Write(“Program finished, press Enter/Return to continue:”); Console.ReadLine(); }
  • 40. Querying Complex Objects • The output: • Why?! – Because ToString() is called for each Customer object. ToString() Is inherited directly from the object class. • Solution: Override the ToString() method. Customers in Asia: CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer CSharpCourse2011.Customer Program finished, press Enter/Return to continue:
  • 41. Querying Complex Objects • From this: • To this: class Customer { public string ID { get; set; } public string City { get; set; } public string Country { get; set; } public string Region { get; set; } public decimal Sales { get; set; } public override string ToString() { return “ID: “ + ID + “ City: “ + City + “ Country: “ + Country + “ Region: “ + Region + “ Sales: “ + Sales; } } class Customer { public string ID { get; set; } public string City { get; set; } public string Country { get; set; } public string Region { get; set; } public decimal Sales { get; set; } }
  • 42. Querying Complex Objects • The output now is: Customers in Asia: ID: B City: Mumbai Country: India Region: Asia Sales: 8888 ID: C City: Karachi Country: Pakistan Region: Asia Sales: 7777 ID: D City: Delhi Country: India Region: Asia Sales: 6666 ID: G City: Seoul Country: Korea Region: Asia Sales: 3333 ID: H City: Istanbul Country: Turkey Region: Asia Sales: 2222 ID: I City: Shanghai Country: China Region: Asia Sales: 1111 ID: L City: Jakarta Country: Indonesia Region: Asia Sales: 3000 ID: M City: Tokyo Country: Japan Region: Asia Sales: 4000 ID: P City: Tehran Country: Iran Region: Asia Sales: 7000 ID: R City: Beijing Country: China Region: Asia Sales: 9000 Program finished, press Enter/Return to continue:
  • 43. Querying Complex Objects var queryResults = from c in customers where c.Region == “Asia” select c.City; Mumbai Karachi Delhi Seoul Istanbul Shanghai Jakarta Tokyo Tehran Beijing
  • 45. Querying Complex Objects • Let’s see this: select c.City, c.Country, c.Sales
  • 46. Querying Complex Objects • Let’s see this: • It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause! – So, what’s the solution? • Just creating a new object on-the-fly in the select clause to hold the results you want for your query! select c.City, c.Country, c.Sales
  • 47. Querying Complex Objects • Let’s see this: • It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause! – So, what’s the solution? • Just creating a new object on-the-fly in the select clause to hold the results you want for your query! select c.City, c.Country, c.Sales var queryResults = from c in customers where c.Region == “North America” select new { c.City, c.Country, c.Sales }; foreach (var item in queryResults) { Console.WriteLine(item); }
  • 48. Querying Complex Objects • Let’s see this: • It’s an error. Unlike in SQL, LINQ does not allow multiple fields in a select clause! – So, what’s the solution? • Just creating a new object on-the-fly in the select clause to hold the results you want for your query! select c.City, c.Country, c.Sales var queryResults = from c in customers where c.Region == “North America” select new { c.City, c.Country, c.Sales }; foreach (var item in queryResults) { Console.WriteLine(item); } { City = New York, Country = USA, Sales = 9999 } { City = Mexico City, Country = Mexico, Sales = 2000 } { City = Los Angeles, Country = USA, Sales = 5000 } Program finished, press Enter/Return to continue:
  • 49. Querying Complex Objects • Now, instead of all this: • Let’s try this: var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales }); var queryResults = from c in customers where c.Region == “North America” select new { c.City, c.Country, c.Sales }; foreach (var item in queryResults) { Console.WriteLine(item); }
  • 50. Querying Complex Objects • Now, instead of all this: • Let’s try this: var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales }); { City = New York, Country = USA, Sales = 9999 } { City = Mexico City, Country = Mexico, Sales = 2000 } { City = Los Angeles, Country = USA, Sales = 5000 } Program finished, press Enter/Return to continue: var queryResults = from c in customers where c.Region == “North America” select new { c.City, c.Country, c.Sales }; foreach (var item in queryResults) { Console.WriteLine(item); }
  • 51. Querying Complex Objects • Now, instead of this: • Let’s try this: var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales }) .Where(c => c.Region == “North America”); var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales });
  • 52. Querying Complex Objects • Now, instead of this: • Let’s try this: • It’s a Compiler Error! Because: The Region property is not included in the anonymous type. {c.City, c.Country, c.Sales } created by the Select() projection. The compiler doesn’t know it yet! var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales }) .Where(c => c.Region == “North America”); var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales });
  • 53. Querying Complex Objects • OK, now, instead of this: • Let’s try this: var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales }) .Where(c => c.City == “New York”); var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales });
  • 54. Querying Complex Objects • OK, now, instead of this: • Let’s try this: • Works beautifully! var queryResults = customers.Select(c => new { c.City, c.Country, c.Sales }) .Where(c => c.City == “New York”); var queryResults = customers.Where(c => c.Region == “North America”) .Select(c => new { c.City, c.Country, c.Sales });
  • 55. Querying Complex Objects • Distinct var queryResults = customers.Select(c => c.Region).Distinct(); var queryResults = (from c in customers select c.Region).Distinct();
  • 57. Ordering By Multiple Levels var queryResults = from c in customers orderby c.Region, c.Country, c.City select new { c.ID, c.Region, c.Country, c.City };
  • 58. Ordering By Multiple Levels var queryResults = from c in customers orderby c.Region, c.Country, c.City select new { c.ID, c.Region, c.Country, c.City }; { ID = O, Region = Africa, Country = Egypt, City = Cairo } { ID = J, Region = Africa, Country = Nigeria, City = Lagos } { ID = R, Region = Asia, Country = China, City = Beijing } { ID = I, Region = Asia, Country = China, City = Shanghai } { ID = D, Region = Asia, Country = India, City = Delhi } { ID = B, Region = Asia, Country = India, City = Mumbai } { ID = L, Region = Asia, Country = Indonesia, City = Jakarta } { ID = P, Region = Asia, Country = Iran, City = Tehran } { ID = M, Region = Asia, Country = Japan, City = Tokyo } { ID = G, Region = Asia, Country = Korea, City = Seoul } ….
  • 59. Drilling further • Many other techniques • Grouping • Joining • Take • Skip • ….