fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Product
  6. {
  7. public string Name { get; set; }
  8. public string Price { get; set; }
  9. }
  10.  
  11. public class Test
  12. {
  13. public static void Main()
  14. {
  15. List<Product> sourceProductList = new List<Product>
  16. {
  17. new Product { Name = "Cornflakes", Price = "1.99M"},
  18. new Product { Name = "Cornflakes", Price = "1.89M"},
  19. new Product { Name = "Rice Krispies", Price = "2.09M"},
  20. new Product { Name = "Cornflakes", Price = "2.09M"},
  21. };
  22.  
  23. var result = sourceProductList
  24. .GroupBy(x => x.Name)
  25. .Where(g => g.Select(x => x.Price).Distinct().Count() == 1)
  26. .Select(g => g.First());
  27.  
  28. foreach (Product product in result)
  29. {
  30. Console.WriteLine("Name: {0}, Price: {1}", product.Name, product.Price);
  31. }
  32. }
  33. }
Success #stdin #stdout 0.05s 37184KB
stdin
stdout
Name: Rice Krispies, Price: 2.09M