Learn C# - LINQ - Cheatsheet - Codecademy
Learn C# - LINQ - Cheatsheet - Codecademy
Cheatsheets / Learn C#
Lists in C#
In C#, a list is a generic data structure that can hold any List<string> names = new List<string>();
type. Use the new operator and declare the element
List<Object> someObjects = new
type in the angle brackets < > .
In the example code, names is a list containing List<Object>();
string values. someObjects is a list containing
Object instances.
Generic Collections
Limitless Lists
Unlike a C# array, a C# list does not have a limited // Initialize array with length 2
number of elements. You can add as many items as you
string[] citiesArray = new string[2];
like.
citiesArray[0] = "Los Angeles";
citiesArray[1] = "New York City";
citiesArray[2] = "Dubai"; // Error!
Count Property
citiesList.Remove("Los Angeles");
Console.WriteLine(citiesList.Count);
// Output: 1
Contains()
In C#, the list method Contains() returns true if its List<string> citiesList = new
argument exists in the list; otherwise, false .
List<string> { "Los Angeles", "New York
In the example code, the first call to Contains()
returns true because “New York City” is in the list. The City", "Dubai" };
second call returns false because “Cairo” is not in the
list. result1 = citiesList.Contains("New York
City");
// result1 is true
result2 = citiesList.Contains("Cairo");
// result2 is false
LINQ
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 2/6
19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy
Using LINQ
var
Since the type of an executed LINQ query’s result is not var custQuery = from cust in customers
always known, it is common to store the result in an
where cust.City ==
implicitly typed variable using the keyword var .
"Phoenix"
select new { cust.Name,
cust.Phone };
// Query syntax
var custQuery =
from cust in customers
where cust.City == "London"
select cust;
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 3/6
19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy
Where
In LINQ queries, the Where operator is used to select List<Customer> customers = new
certain elements from a sequence.
List<Customer>
It expects an expression that evaluates to a
boolean value. {
Every element satisfying the condition will be new Customer("Bartleby", "London"),
included in the resulting query.
new Customer("Benjamin",
It can be used in both method syntax and query
syntax. "Philadelphia"),
new Customer("Michelle", "Busan" )
};
// Query syntax
var custQuery =
from cust in customers
where cust.City == "London"
select cust;
// Method syntax
var custQuery2 = customers.Where(cust =>
cust.City == "London");
From
In LINQ queries, the from operator declares a range string[] names = { "Hansel", "Gretel",
variable that is used to traverse the sequence. It is only
"Helga", "Gus" };
used in query syntax.
In the example code, n represents each element in
names . The returned query only contains those var query =
elements for which n.Contains("a") is true. from n in names
where n.Contains("a")
select n;
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 4/6
19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy
Select
In LINQ queries, the Select operator determines what string[] trees = { "Elm", "Banyon",
is returned for each element in the resulting query. It
"Rubber" };
can be used in both method and query syntax.
// Query syntax
var treeQuery =
from t in trees
select t.ToUpper();
// Method syntax
var treeQuery2 = names.Select(t =>
t.ToUpper());
You can use a foreach loop to iterate over the result string[] names = { "Hansel", "Gretel",
of an executed LINQ query.
"Helga", "Gus" };
In the example code, query is the result of a LINQ
query, and it can be iterated over using foreach .
name represents each element in names . var query = names.Where(n =>
n.Contains("a"));
Count()
The result of an executed LINQ query has a method string[] names = { "Hansel", "Gretel",
Count() , which returns the number of elements it
"Helga", "Gus" };
contains.
In the example code, Count() returns 2 because the
resulting query contains 2 elements containing “a”. var query = names.Where(x =>
x.Contains("a"));
Console.WriteLine(query.Count());
// Output: 2
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 5/6
19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 6/6