-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinq_Methods.cs
More file actions
38 lines (36 loc) · 1.51 KB
/
Copy pathLinq_Methods.cs
File metadata and controls
38 lines (36 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class LinqMethods
{
public static void LinqNumbers()
{
List<int> scores = [97, 92, 81, 60, 65];
// IEnumerable<int> scoresQuery =
// from score in scores
// where score > 80 && score < 95
// orderby score ascending
// select score;
//below is the same code as above, but with a Lambda query (arrow function in JS or ternary operators)
var scoresQuery = scores.Where(s => s > 80 && s < 95).OrderBy(s => s); //it's so simple even the Select is implied
//System.Console.WriteLine(scoresQuery.Count());//shows the number of occurences
List<int> highScores = scoresQuery.ToList();//stores the query results into a list of same Type
foreach (var i in scoresQuery)
{
System.Console.WriteLine(i);
}
}
public static void LinqStrings()
{
List<string> cars = ["Voyage", "March", "Celta", "Sentra", "Civic", "Corolla", "Chevette", "Pampa", "Prisma"];
/*IEnumerable<string> carsQuery =
from car in cars
where car.StartsWith("C")
orderby car ascending
select $"This is the car you want: {car}";
*/
var carsQuery = cars.Where(car => car.StartsWith("C")).OrderBy(car => car);
List<string> carsStartsWithC = carsQuery.ToList();//a list of cars that only starts with C
foreach (var i in carsStartsWithC)
{
System.Console.WriteLine(i);
}
}
}