Skip to content

Commit 16da6c4

Browse files
Merge pull request #12565 from dotnet/main
Merge main into live
2 parents d56e617 + 6148ab4 commit 16da6c4

90 files changed

Lines changed: 481 additions & 374 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SequenceExamples
6+
{
7+
class Program
8+
{
9+
// This part is just for testing the examples
10+
static void Main(string[] args)
11+
{
12+
AggregateBy.AggregateBySeedExample();
13+
}
14+
15+
#region AggregateBy
16+
static class AggregateBy
17+
{
18+
19+
public static void AggregateBySeedSelectorExample()
20+
{
21+
// <Snippet205>
22+
(string Name, string Department, decimal Salary)[] employees =
23+
{
24+
("Ali", "HR", 45000),
25+
("Samer", "Technology", 50000),
26+
("Hamed", "Sales", 75000),
27+
("Lina", "Technology", 65000),
28+
("Omar", "HR", 40000)
29+
};
30+
31+
var result =
32+
employees.AggregateBy(
33+
e => e.Department,
34+
dept => (Total: 0m, Count: 0),
35+
(acc, e) => (acc.Total + e.Salary, acc.Count + 1)
36+
);
37+
38+
foreach (var item in result)
39+
{
40+
Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
41+
}
42+
43+
/*
44+
This code produces the following output:
45+
46+
HR: Total=85000, Count=2
47+
Technology: Total=115000, Count=2
48+
Sales: Total=75000, Count=1
49+
*/
50+
// </Snippet205>
51+
}
52+
53+
54+
55+
public static void AggregateBySeedExample()
56+
{
57+
// <Snippet206>
58+
(string Name, string Department, decimal Salary)[] employees =
59+
{
60+
("Ali", "HR", 45000),
61+
("Samer", "Technology", 50000),
62+
("Hamed", "Sales", 75000),
63+
("Lina", "Technology", 65000),
64+
("Omar", "HR", 40000)
65+
};
66+
67+
var totals =
68+
employees.AggregateBy(
69+
e => e.Department,
70+
0m,
71+
(total, e) => total + e.Salary
72+
);
73+
74+
foreach (var item in totals)
75+
{
76+
Console.WriteLine($"{item.Key}: {item.Value}");
77+
}
78+
79+
/*
80+
This code produces the following output:
81+
82+
HR: 85000
83+
Technology: 115000
84+
Sales: 75000
85+
*/
86+
// </Snippet206>
87+
}
88+
89+
}
90+
#endregion
91+
}
92+
}

snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs

Lines changed: 0 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,157 +3205,5 @@ static void TakeLast()
32053205
}
32063206
#endregion
32073207

3208-
#region AggregateBy
3209-
static class AggregateBy
3210-
{
3211-
// <Snippet205>
3212-
public static void AggregateBySeedSelectorExample()
3213-
{
3214-
(string Name, string Department, decimal Salary)[] employees =
3215-
{
3216-
("Ali", "HR", 45000),
3217-
("Samer", "Technology", 50000),
3218-
("Hamed", "Sales", 75000),
3219-
("Lina", "Technology", 65000),
3220-
("Omar", "HR", 40000)
3221-
};
3222-
3223-
var result =
3224-
employees.AggregateBy(
3225-
e => e.Department,
3226-
dept => (Total: 0m, Count: 0),
3227-
(acc, e) => (acc.Total + e.Salary, acc.Count + 1)
3228-
);
3229-
3230-
foreach (var item in result)
3231-
{
3232-
Console.WriteLine($"{item.Key}: Total={item.Value.Total}, Count={item.Value.Count}");
3233-
}
3234-
3235-
/*
3236-
This code produces the following output:
3237-
3238-
HR: Total=85000, Count=2
3239-
Technology: Total=115000, Count=2
3240-
Sales: Total=75000, Count=1
3241-
*/
3242-
}
3243-
// </Snippet205>
3244-
3245-
// <Snippet206>
3246-
public static void AggregateBySeedExample()
3247-
{
3248-
(string Name, string Department, decimal Salary)[] employees =
3249-
{
3250-
("Ali", "HR", 45000),
3251-
("Samer", "Technology", 50000),
3252-
("Hamed", "Sales", 75000),
3253-
("Lina", "Technology", 65000),
3254-
("Omar", "HR", 40000)
3255-
};
3256-
3257-
var totals =
3258-
employees.AggregateBy(
3259-
e => e.Department,
3260-
0m,
3261-
(total, e) => total + e.Salary
3262-
);
3263-
3264-
foreach (var item in totals)
3265-
{
3266-
Console.WriteLine($"{item.Key}: {item.Value}");
3267-
}
3268-
3269-
/*
3270-
This code produces the following output:
3271-
3272-
HR: 85000
3273-
Technology: 115000
3274-
Sales: 75000
3275-
*/
3276-
}
3277-
// </Snippet206>
3278-
}
3279-
#endregion
3280-
3281-
#region UnionBy
3282-
static class UnionBy
3283-
{
3284-
// <Snippet207>
3285-
public static void UnionByKeySelectorExample()
3286-
{
3287-
(int ProductId, string Name , decimal Price)[] localProducts =
3288-
{
3289-
(101, "Laptop", 1000m),
3290-
(102, "Mouse", 100m),
3291-
(103, "Keyboard", 120m)
3292-
};
3293-
3294-
(int ProductId, string Name, decimal Price)[] warehouseProducts =
3295-
{
3296-
(102, "Mouse", 100m), // Duplicate ProductId (already in local)
3297-
(104, "Monitor", 800m),
3298-
(101, "Laptop", 1000m) // Duplicate ProductId (already in local)
3299-
};
3300-
var combinedProducts =
3301-
localProducts.UnionBy(
3302-
warehouseProducts,
3303-
product => product.ProductId
3304-
);
3305-
3306-
foreach (var product in combinedProducts)
3307-
{
3308-
Console.WriteLine($"{product.ProductId}: {product.Name} - ${product.Price}");
3309-
}
3310-
3311-
/*
3312-
This code produces the following output:
3313-
3314-
101: Laptop - $1000
3315-
102: Mouse - $100
3316-
103: Keyboard - $120
3317-
104: Monitor - $800
3318-
*/
3319-
}
3320-
// </Snippet207>
3321-
3322-
// <Snippet208>
3323-
public static void UnionByComparerExample()
3324-
{
3325-
(string Email, string FullName)[] marketingList =
3326-
{
3327-
("Mahmoud.Doe@example.com", "Mahmoud Doe"),
3328-
("alice.smith@example.com", "Alice Smith")
3329-
};
3330-
3331-
(string Email, string FullName)[] salesList =
3332-
{
3333-
("ALICE.SMITH@EXAMPLE.COM", "Alice S."), // Duplicate email, different casing
3334-
("Sara.jones@example.com", "Sara Jones")
3335-
};
3336-
3337-
var combinedList =
3338-
marketingList.UnionBy(
3339-
salesList,
3340-
contact => contact.Email,
3341-
StringComparer.OrdinalIgnoreCase
3342-
);
3343-
3344-
foreach (var contact in combinedList)
3345-
{
3346-
Console.WriteLine($"{contact.FullName} ({contact.Email})");
3347-
}
3348-
3349-
/*
3350-
This code produces the following output:
3351-
3352-
Mahmoud Doe (Mahmoud.Doe@example.com)
3353-
Alice Smith (alice.smith@example.com)
3354-
Sara Jones (Sara.jones@example.com)
3355-
*/
3356-
}
3357-
// </Snippet208>
3358-
}
3359-
#endregion
33603208
}
33613209
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SequenceExamples
6+
{
7+
class Program
8+
{
9+
// This part is just for testing the examples
10+
static void Main(string[] args)
11+
{
12+
CountBy.CountByDepartmentExample();
13+
}
14+
15+
#region CountBy
16+
static class CountBy
17+
{
18+
19+
public static void CountByDepartmentExample()
20+
{
21+
// <Snippet209>
22+
(string Name, int Age, string Department)[] employees =
23+
{
24+
("Saly", 23, "IT"),
25+
("David", 25, "Sales"),
26+
("Mahmoud", 22, "IT"),
27+
("Qamar", 22, "HR"),
28+
("Sara", 25, "IT"),
29+
("John", 26, "HR"),
30+
("Jaffar", 32, "Sales")
31+
};
32+
33+
// Count the number of employees per department
34+
var countPerDepartment = employees.CountBy(employee => employee.Department);
35+
36+
foreach (var item in countPerDepartment)
37+
{
38+
Console.WriteLine($"Department: {item.Key} - Employees Count: {item.Value}");
39+
}
40+
41+
/*
42+
This code produces the following output:
43+
44+
Department: IT - Employees Count: 3
45+
Department: Sales - Employees Count: 2
46+
Department: HR - Employees Count: 2
47+
*/
48+
// </Snippet209>
49+
}
50+
51+
}
52+
#endregion
53+
}
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)