-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathenumerable.cs
More file actions
92 lines (77 loc) · 2.62 KB
/
Copy pathenumerable.cs
File metadata and controls
92 lines (77 loc) · 2.62 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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)
{
AggregateBy.AggregateBySeedExample();
}
#region AggregateBy
static class AggregateBy
{
public static void AggregateBySeedSelectorExample()
{
// <Snippet205>
(string Name, string Department, decimal Salary)[] employees =
{
("Ali", "HR", 45000),
("Samer", "Technology", 50000),
("Hamed", "Sales", 75000),
("Lina", "Technology", 65000),
("Omar", "HR", 40000)
};
var result =
employees.AggregateBy(
e => e.Department,
dept => (Total: 0m, Count: 0),
(acc, e) => (acc.Total + e.Salary, acc.Count + 1)
);
foreach (var item in result)
{
Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
}
/*
This code produces the following output:
HR: Total=85000, Count=2
Technology: Total=115000, Count=2
Sales: Total=75000, Count=1
*/
// </Snippet205>
}
public static void AggregateBySeedExample()
{
// <Snippet206>
(string Name, string Department, decimal Salary)[] employees =
{
("Ali", "HR", 45000),
("Samer", "Technology", 50000),
("Hamed", "Sales", 75000),
("Lina", "Technology", 65000),
("Omar", "HR", 40000)
};
var totals =
employees.AggregateBy(
e => e.Department,
0m,
(total, e) => total + e.Salary
);
foreach (var item in totals)
{
Console.WriteLine($"{item.Key}: {item.Value}");
}
/*
This code produces the following output:
HR: 85000
Technology: 115000
Sales: 75000
*/
// </Snippet206>
}
}
#endregion
}
}