Exercise: Food Delivery Revenue Analysis

Group delivery orders by restaurant and calculate total revenue using anonymous types and aggregation.

Problem statement

You are analyzing the daily transaction logs for a local food delivery platform. You have a raw, flat list of completed orders from various restaurants. To generate the daily payout report, you must group these individual orders by their respective restaurants and calculate the total money earned by each establishment.

Task requirements

  • Organize the raw list of orders by the restaurant’s name.

  • Calculate the total daily revenue for each restaurant by summing the cost of all its associated orders.

  • Create a new collection of custom objects containing exactly two properties: the restaurant’s name and its total calculated revenue.

  • Iterate through the results and print the name and total revenue for each restaurant to the console.

Constraints

  • You must use the GroupBy() method to group the data by the restaurant name.

  • You must use the Select() method combined with the new { ... } syntax to project a collection of anonymous types.

  • You must use the Sum() method inside your projection logic to calculate the total revenue for the group.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • When using GroupBy(o => o.RestaurantName), the resulting sequence contains IGrouping objects where the Key property represents the specific restaurant name.

  • In your Select clause, you are iterating over groups. You can call .Sum(o => o.OrderTotal) directly on the group itself to aggregate the values of the items inside that group.

  • Anonymous types are created using curly braces without a class name: select new { Name = ..., Revenue = ... }.

Exercise: Food Delivery Revenue Analysis

Group delivery orders by restaurant and calculate total revenue using anonymous types and aggregation.

Problem statement

You are analyzing the daily transaction logs for a local food delivery platform. You have a raw, flat list of completed orders from various restaurants. To generate the daily payout report, you must group these individual orders by their respective restaurants and calculate the total money earned by each establishment.

Task requirements

  • Organize the raw list of orders by the restaurant’s name.

  • Calculate the total daily revenue for each restaurant by summing the cost of all its associated orders.

  • Create a new collection of custom objects containing exactly two properties: the restaurant’s name and its total calculated revenue.

  • Iterate through the results and print the name and total revenue for each restaurant to the console.

Constraints

  • You must use the GroupBy() method to group the data by the restaurant name.

  • You must use the Select() method combined with the new { ... } syntax to project a collection of anonymous types.

  • You must use the Sum() method inside your projection logic to calculate the total revenue for the group.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • When using GroupBy(o => o.RestaurantName), the resulting sequence contains IGrouping objects where the Key property represents the specific restaurant name.

  • In your Select clause, you are iterating over groups. You can call .Sum(o => o.OrderTotal) directly on the group itself to aggregate the values of the items inside that group.

  • Anonymous types are created using curly braces without a class name: select new { Name = ..., Revenue = ... }.

C# 14.0
using FoodDelivery;
// 1. Attaining the data source
var dailyOrders = new List<Order>
{
new Order { RestaurantName = "Burger Joint", Item = "Cheeseburger", OrderTotal = 12.50m },
new Order { RestaurantName = "Pizza Palace", Item = "Pepperoni Pizza", OrderTotal = 22.00m },
new Order { RestaurantName = "Burger Joint", Item = "Fries and Shake", OrderTotal = 8.75m },
new Order { RestaurantName = "Sushi Central", Item = "Spicy Tuna Roll", OrderTotal = 15.00m },
new Order { RestaurantName = "Pizza Palace", Item = "Garlic Bread", OrderTotal = 5.50m },
new Order { RestaurantName = "Sushi Central", Item = "Sashimi Platter", OrderTotal = 35.00m }
};
// 2. Create your LINQ query here
// var revenueReport = ...
// 3. Execute the query and print the results
Console.WriteLine("Daily Revenue Report:");
// foreach(...) { ... }