Advertisement
Guest User

Untitled

a guest
May 13th, 2013
2,400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             List<CartLine> Lines = new List<CartLine>();
  6.             Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
  7.             Lines.Add(new CartLine() { ProductCode = "p1", Price = 6.5M, Name = "Product1" });
  8.             Lines.Add(new CartLine() { ProductCode = "p2", Price = 12M, Name = "Product2" });
  9.  
  10.  
  11.             var result = Lines
  12.                    .GroupBy(l => l.ProductCode)
  13.                    .SelectMany(cl => cl.Select(
  14.                        csLine => new CartSummaryLine
  15.                        {
  16.                            Name = csLine.Name,
  17.                            Quantity = cl.Count().ToString(),
  18.                            TotalPrice = cl.Sum(c => c.Price).ToString(),
  19.                        }));
  20.  
  21.             foreach (var l in result)
  22.             {
  23.                 Console.WriteLine("Name:" + l.Name + "\tQuantity: " + l.Quantity + "\tPrice: " + l.TotalPrice);
  24.             }
  25.             Console.ReadLine();
  26.         }
  27.     }
  28.  
  29.     public class CartLine
  30.     {
  31.         public CartLine() { }
  32.  
  33.         public string ProductCode { get; set; }
  34.         public decimal Price { get; set; }
  35.         public string Name { get; set; }
  36.     }
  37.  
  38.     public class CartSummaryLine
  39.     {
  40.         public CartSummaryLine() { }
  41.  
  42.         public string Name { get; set; }
  43.         public string Quantity { get; set; }
  44.         public string TotalPrice { get; set; }
  45.  
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement