-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathenumerable.cs
More file actions
70 lines (60 loc) · 2.18 KB
/
Copy pathenumerable.cs
File metadata and controls
70 lines (60 loc) · 2.18 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
namespace SequenceExamples
{
class Program
{
// This part is just for testing the examples
static void Main(string[] args)
{
MaxBy.MaxByKeySelectorExample();
MaxBy.MaxByComparerExample();
}
#region MaxBy
static class MaxBy
{
public static void MaxByKeySelectorExample()
{
// <Snippet212>
(string Name, decimal Salary, int Age)[] employees =
{
("Mahmoud", 1000m, 22),
("John", 1200m, 28),
("Sara", 2000m, 32),
("Hadi", 1750m, 27),
("Lana", 1500m, 24),
("Luna", 1850m, 33)
};
// Get the oldest employee in the company.
var oldestEmployee = employees.MaxBy(employee => employee.Age);
Console.WriteLine($"Name: {oldestEmployee.Name}, Age: {oldestEmployee.Age}, Salary: ${oldestEmployee.Salary}");
/*
This code produces the following output:
Name: Luna, Age: 33, Salary: $1850
*/
// </Snippet212>
}
public static void MaxByComparerExample()
{
// <Snippet213>
(string Name, int Quantity)[] inventory =
{
("apple", 10),
("BANANA", 5),
("Cherry", 20)
};
// Find the product with the maximum name alphabetically, ignoring casing differences.
// 'C' is correctly identified as greater than 'a' and 'B' when case is ignored.
var maxIgnoreCase = inventory.MaxBy(item => item.Name, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Case-insensitive comparison: {maxIgnoreCase.Name}");
/*
This code produces the following output:
Case-insensitive comparison: Cherry
*/
// </Snippet213>
}
}
#endregion
}
}