Skip to content

Commit 26802a1

Browse files
Add minby maxby examples (#12562)
1 parent bba190c commit 26802a1

5 files changed

Lines changed: 226 additions & 18 deletions

File tree

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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
MaxBy.MaxByKeySelectorExample();
13+
MaxBy.MaxByComparerExample();
14+
}
15+
16+
#region MaxBy
17+
static class MaxBy
18+
{
19+
public static void MaxByKeySelectorExample()
20+
{
21+
// <Snippet212>
22+
(string Name, decimal Salary, int Age)[] employees =
23+
{
24+
("Mahmoud", 1000m, 22),
25+
("John", 1200m, 28),
26+
("Sara", 2000m, 32),
27+
("Hadi", 1750m, 27),
28+
("Lana", 1500m, 24),
29+
("Luna", 1850m, 33)
30+
};
31+
32+
// Get the oldest employee in the company.
33+
var oldestEmployee = employees.MaxBy(employee => employee.Age);
34+
35+
Console.WriteLine($"Name: {oldestEmployee.Name}, Age: {oldestEmployee.Age}, Salary: ${oldestEmployee.Salary}");
36+
37+
/*
38+
This code produces the following output:
39+
40+
Name: Luna, Age: 33, Salary: $1850
41+
*/
42+
// </Snippet212>
43+
}
44+
45+
public static void MaxByComparerExample()
46+
{
47+
// <Snippet213>
48+
(string Name, int Quantity)[] inventory =
49+
{
50+
("apple", 10),
51+
("BANANA", 5),
52+
("Cherry", 20)
53+
};
54+
55+
// Find the product with the maximum name alphabetically, ignoring casing differences.
56+
// 'C' is correctly identified as greater than 'a' and 'B' when case is ignored.
57+
var maxIgnoreCase = inventory.MaxBy(item => item.Name, StringComparer.OrdinalIgnoreCase);
58+
Console.WriteLine($"Case-insensitive comparison: {maxIgnoreCase.Name}");
59+
60+
/*
61+
This code produces the following output:
62+
63+
Case-insensitive comparison: Cherry
64+
*/
65+
// </Snippet213>
66+
}
67+
}
68+
#endregion
69+
}
70+
}
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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
MinBy.MinByKeySelectorExample();
13+
MinBy.MinByComparerExample();
14+
}
15+
16+
#region MinBy
17+
static class MinBy
18+
{
19+
public static void MinByKeySelectorExample()
20+
{
21+
// <Snippet210>
22+
(string Name, decimal Salary, int Age)[] employees =
23+
{
24+
("Mahmoud", 1000m, 22),
25+
("John", 1200m, 28),
26+
("Sara", 2000m, 32),
27+
("Hadi", 1750m, 27),
28+
("Lana", 1500m, 24),
29+
("Luna", 1850m, 33)
30+
};
31+
32+
// Get the youngest employee in the company.
33+
var youngestEmployee = employees.MinBy(employee => employee.Age);
34+
35+
Console.WriteLine($"Name: {youngestEmployee.Name}, Age: {youngestEmployee.Age}, Salary: ${youngestEmployee.Salary}");
36+
37+
/*
38+
This code produces the following output:
39+
40+
Name: Mahmoud, Age: 22, Salary: $1000
41+
*/
42+
// </Snippet210>
43+
}
44+
45+
public static void MinByComparerExample()
46+
{
47+
// <Snippet211>
48+
(string Name, int Quantity)[] inventory =
49+
{
50+
("apple", 10),
51+
("BANANA", 5),
52+
("Cherry", 20)
53+
};
54+
55+
// Find the product with the minimum name alphabetically, ignoring casing differences.
56+
// 'a' is correctly identified as smaller than 'B' when case is ignored.
57+
var minIgnoreCase = inventory.MinBy(item => item.Name, StringComparer.OrdinalIgnoreCase);
58+
Console.WriteLine($"Case-insensitive comparison: {minIgnoreCase.Name}");
59+
60+
/*
61+
This code produces the following output:
62+
63+
Case-insensitive comparison: apple
64+
*/
65+
// </Snippet211>
66+
}
67+
}
68+
#endregion
69+
}
70+
}

xml/System.Linq/Enumerable.xml

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9094,11 +9094,24 @@ The following code example demonstrates how to use <xref:System.Linq.Enumerable.
90949094
<param name="keySelector">A function to extract the key for each element.</param>
90959095
<summary>Returns the maximum value in a generic sequence according to a specified key selector function.</summary>
90969096
<returns>The value with the maximum key in the sequence.</returns>
9097-
<remarks>
9098-
<para>If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.</para>
9099-
<para>If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.</para>
9100-
</remarks>
9101-
<exception cref="T:System.ArgumentNullException">
9097+
<remarks>
9098+
<para>
9099+
If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.
9100+
</para>
9101+
<para>
9102+
If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.
9103+
</para>
9104+
<format type="text/markdown"><![CDATA[
9105+
9106+
## Examples
9107+
9108+
The following code example demonstrates how to use `MaxBy` to find the maximum value in a collection based on a specific property.
9109+
9110+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/MaxBy/enumerable.cs" id="Snippet212":::
9111+
9112+
]]></format>
9113+
</remarks>
9114+
<exception cref="T:System.ArgumentNullException">
91029115
<paramref name="source" /> is <see langword="null" />.</exception>
91039116
<exception cref="T:System.ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="T:System.IComparable" /> or <see cref="T:System.IComparable`1" /> interface.</exception>
91049117
<exception cref="T:System.InvalidOperationException">
@@ -9169,11 +9182,24 @@ The following code example demonstrates how to use <xref:System.Linq.Enumerable.
91699182
<param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1" /> to compare keys.</param>
91709183
<summary>Returns the maximum value in a generic sequence according to a specified key selector function and key comparer.</summary>
91719184
<returns>The value with the maximum key in the sequence.</returns>
9172-
<remarks>
9173-
<para>If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.</para>
9174-
<para>If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.</para>
9175-
</remarks>
9176-
<exception cref="T:System.ArgumentNullException">
9185+
<remarks>
9186+
<para>
9187+
If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.
9188+
</para>
9189+
<para>
9190+
If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.
9191+
</para>
9192+
<format type="text/markdown"><![CDATA[
9193+
9194+
## Examples
9195+
9196+
The following code example demonstrates how to use `MaxBy` with a custom comparer to ignore case sensitivity when checking for the maximum value.
9197+
9198+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/MaxBy/enumerable.cs" id="Snippet213":::
9199+
9200+
]]></format>
9201+
</remarks>
9202+
<exception cref="T:System.ArgumentNullException">
91779203
<paramref name="source" /> is <see langword="null" />.</exception>
91789204
<exception cref="T:System.ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="T:System.IComparable" /> or <see cref="T:System.IComparable`1" /> interface.</exception>
91799205
<exception cref="T:System.InvalidOperationException">
@@ -10974,10 +11000,23 @@ The following code example demonstrates how to use <xref:System.Linq.Enumerable.
1097411000
<param name="keySelector">A function to extract the key for each element.</param>
1097511001
<summary>Returns the minimum value in a generic sequence according to a specified key selector function.</summary>
1097611002
<returns>The value with the minimum key in the sequence.</returns>
10977-
<remarks>
10978-
<para>If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.</para>
10979-
<para>If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.</para>
10980-
</remarks>
11003+
<remarks>
11004+
<para>
11005+
If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.
11006+
</para>
11007+
<para>
11008+
If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.
11009+
</para>
11010+
<format type="text/markdown"><![CDATA[
11011+
11012+
## Examples
11013+
11014+
The following code example demonstrates how to use `MinBy` to find the minimum value in a collection based on a specific property.
11015+
11016+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/MinBy/enumerable.cs" id="Snippet210":::
11017+
11018+
]]></format>
11019+
</remarks>
1098111020
<exception cref="T:System.ArgumentNullException">
1098211021
<paramref name="source" /> is <see langword="null" />.</exception>
1098311022
<exception cref="T:System.ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="T:System.IComparable" /> or <see cref="T:System.IComparable`1" /> interface.</exception>
@@ -11049,10 +11088,23 @@ The following code example demonstrates how to use <xref:System.Linq.Enumerable.
1104911088
<param name="comparer">The <see cref="T:System.Collections.Generic.IComparer`1" /> to compare keys.</param>
1105011089
<summary>Returns the minimum value in a generic sequence according to a specified key selector function and key comparer.</summary>
1105111090
<returns>The value with the minimum key in the sequence.</returns>
11052-
<remarks>
11053-
<para>If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.</para>
11054-
<para>If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.</para>
11055-
</remarks>
11091+
<remarks>
11092+
<para>
11093+
If the source sequence is empty, two possible outcomes are possible depending on the source type. If <typeparamref name="TSource" /> is a nullable type, this method returns <see langword="null" />. If <typeparamref name="TSource" /> is a non-nullable struct, such as a primitive type, an <see cref="T:System.InvalidOperationException" /> is thrown.
11094+
</para>
11095+
<para>
11096+
If the source sequence contains only values that are <see langword="null" />, this method returns <see langword="null" />.
11097+
</para>
11098+
<format type="text/markdown"><![CDATA[
11099+
11100+
## Examples
11101+
11102+
The following code example demonstrates how to use `MinBy` with a custom comparer to ignore case sensitivity when checking for the minimum value.
11103+
11104+
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/MinBy/enumerable.cs" id="Snippet211":::
11105+
11106+
]]></format>
11107+
</remarks>
1105611108
<exception cref="T:System.ArgumentNullException">
1105711109
<paramref name="source" /> is <see langword="null" />.</exception>
1105811110
<exception cref="T:System.ArgumentException">No key extracted from <paramref name="source" /> implements the <see cref="T:System.IComparable" /> or <see cref="T:System.IComparable`1" /> interface.</exception>

0 commit comments

Comments
 (0)