Exercise: Food Delivery Revenue Analysis
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 thenew { ... }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 containsIGroupingobjects where theKeyproperty represents the specific restaurant name.In your
Selectclause, 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
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 thenew { ... }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 containsIGroupingobjects where theKeyproperty represents the specific restaurant name.In your
Selectclause, 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 = ... }.