Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
92 changes: 92 additions & 0 deletions snippets/csharp/System.Linq/Enumerable/AggregateBy/enumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}
152 changes: 0 additions & 152 deletions snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3205,157 +3205,5 @@ static void TakeLast()
}
#endregion

#region AggregateBy
static class AggregateBy
{
// <Snippet205>
public static void AggregateBySeedSelectorExample()
{
(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>

// <Snippet206>
public static void AggregateBySeedExample()
{
(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

#region UnionBy
static class UnionBy
{
// <Snippet207>
public static void UnionByKeySelectorExample()
{
(int ProductId, string Name , decimal Price)[] localProducts =
{
(101, "Laptop", 1000m),
(102, "Mouse", 100m),
(103, "Keyboard", 120m)
};

(int ProductId, string Name, decimal Price)[] warehouseProducts =
{
(102, "Mouse", 100m), // Duplicate ProductId (already in local)
(104, "Monitor", 800m),
(101, "Laptop", 1000m) // Duplicate ProductId (already in local)
};
var combinedProducts =
localProducts.UnionBy(
warehouseProducts,
product => product.ProductId
);

foreach (var product in combinedProducts)
{
Console.WriteLine($"{product.ProductId}: {product.Name} - ${product.Price}");
}

/*
This code produces the following output:

101: Laptop - $1000
102: Mouse - $100
103: Keyboard - $120
104: Monitor - $800
*/
}
// </Snippet207>

// <Snippet208>
public static void UnionByComparerExample()
{
(string Email, string FullName)[] marketingList =
{
("Mahmoud.Doe@example.com", "Mahmoud Doe"),
("alice.smith@example.com", "Alice Smith")
};

(string Email, string FullName)[] salesList =
{
("ALICE.SMITH@EXAMPLE.COM", "Alice S."), // Duplicate email, different casing
("Sara.jones@example.com", "Sara Jones")
};

var combinedList =
marketingList.UnionBy(
salesList,
contact => contact.Email,
StringComparer.OrdinalIgnoreCase
);

foreach (var contact in combinedList)
{
Console.WriteLine($"{contact.FullName} ({contact.Email})");
}

/*
This code produces the following output:

Mahmoud Doe (Mahmoud.Doe@example.com)
Alice Smith (alice.smith@example.com)
Sara Jones (Sara.jones@example.com)
*/
}
// </Snippet208>
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
54 changes: 54 additions & 0 deletions snippets/csharp/System.Linq/Enumerable/CountBy/enumerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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)
{
CountBy.CountByDepartmentExample();
}

#region CountBy
static class CountBy
{

public static void CountByDepartmentExample()
{
// <Snippet209>
(string Name, int Age, string Department)[] employees =
{
("Saly", 23, "IT"),
("David", 25, "Sales"),
("Mahmoud", 22, "IT"),
("Qamar", 22, "HR"),
("Sara", 25, "IT"),
("John", 26, "HR"),
("Jaffar", 32, "Sales")
};

// Count the number of employees per department
var countPerDepartment = employees.CountBy(employee => employee.Department);

foreach (var item in countPerDepartment)
{
Console.WriteLine($"Department: {item.Key} - Employees Count: {item.Value}");
}

/*
This code produces the following output:

Department: IT - Employees Count: 3
Department: Sales - Employees Count: 2
Department: HR - Employees Count: 2
*/
// </Snippet209>
}

}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
Loading
Loading