0% found this document useful (0 votes)
17 views6 pages

Learn C# - LINQ - Cheatsheet - Codecademy

C# sheet for programmers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Learn C# - LINQ - Cheatsheet - Codecademy

C# sheet for programmers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy

Cheatsheets / Learn C#

Learn C#: LINQ

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

Some collections, like lists and dictionaries, can be using System.Collections.Generic;


associated with various types. Instead of defining a
unique class for each possible type, we define them
with a generic type T , e.g. List<T> . List<string> names = new List<string>();
These collections are called generic collection types. List<Object> objs = new List<Object>();
They are available in the
Dictionary<string,int> scores = new
System.Collections.Generic namespace.
The generic type T will often show up in Dictionary<string, int>();
documentation. When using a generic collection in your
code, the actual type is specified when the collection is
declared or instantiated.

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!

// Initialize list; no length needed


List<string> citiesList = new
List<string>();
citiesList.Add("Los Angeles");
citiesList.Add("New York City");
citiesList.Add("Dubai");
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-linq/cheatsheet 1/6
19/11/2024, 10:15 Learn C#: Learn C#: LINQ Cheatsheet | Codecademy

Count Property

The number of elements in a list is stored in the List<string> citiesList = new


Count property.
List<string>();
In the example code, the Count of citiesList
changes as we add and remove values. citiesList.Add("Los Angeles");
Console.WriteLine(citiesList.Count);
// Output: 1

citiesList.Add("New York City");


Console.WriteLine(citiesList.Count);
// Output: 2

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

LINQ is a set of language and framework features for


writing queries on collection types. It is useful for
selecting, accessing, and transforming data in a dataset.

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

LINQ features can be used in a C# program by using System.Linq;


importing the System.Linq namespace.

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 };

Method & Query Syntax

In C#, LINQ queries can be written in method syntax or // Method syntax


query syntax.
var custQuery2 = customers.Where(cust =>
Method syntax resembles most other C# method calls,
while query syntax resembles SQL. cust.City == "London");

// 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");

// Result: Customer("Bartleby", "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;

// Result: Hansel, Helga

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());

// Result: ELM, BANYON, RUBBER

LINQ & foreach

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"));

foreach (var name in query)


{
Console.WriteLine(name);
}

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

You might also like