Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- List<CartLine> Lines = new List<CartLine>();
- Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
- Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
- Lines.Add(new CartLine() { ProductCode = "p2", Price = 12M, Name = "Product2" });
- var result = Lines
- .GroupBy(l => l.ProductCode)
- .SelectMany(cl => cl.Select(
- csLine => new CartSummaryLine
- {
- Name = csLine.Name,
- Quantity = cl.Count().ToString(),
- TotalPrice = cl.Sum(c => c.Price).ToString(),
- }));
- foreach (var l in result)
- {
- Console.WriteLine("Name:" + l.Name + "\tQuantity: " + l.Quantity + "\tPrice: " + l.TotalPrice);
- }
- Console.ReadLine();
- }
- }
- public class CartLine
- {
- public CartLine() { }
- public string ProductCode { get; set; }
- public decimal Price { get; set; }
- public string Name { get; set; }
- }
- public class CartSummaryLine
- {
- public CartSummaryLine() { }
- public string Name { get; set; }
- public string Quantity { get; set; }
- public string TotalPrice { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement