28
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
10
//Create a list of products
11
var products = new List<Product>();
12
products.Add(new Product { Name = "PS4", Quantity = 3, Price = 399.99m });
13
products.Add(new Product { Name = "Xbox One", Quantity = 10, Price = 399.99m });
14
products.Add(new Product { Name = "Wii U", Quantity = 5, Price = 299.99m });
15
16
//Calculate total (qty * price)
17
var total = products.Aggregate(0m , (current, p) => current + p.Quantity * p.Price);
18
19
Console.WriteLine(total);
20
}
21
22
//Define Product Class
23
public class Product {
24
public string Name {get; set; }
25
public int Quantity {get; set; }
26
public decimal Price {get; set; }
27
}
28
}
Cached Result