Mega Code Archive

 
Categories / C# Tutorial / LINQ
 

Display Products With Unknown Price

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq;     class ProductWithNullablePrice     {         public string Name { get; private set; }         public decimal? Price { get; private set; }         public ProductWithNullablePrice(string name, decimal price)         {             Name = name;             Price = price;         }         ProductWithNullablePrice()         {         }         public static List<ProductWithNullablePrice> GetSampleProducts()         {             return new List<ProductWithNullablePrice>             {                 new ProductWithNullablePrice { Name="C", Price= 9.99m },                 new ProductWithNullablePrice { Name="A", Price= 4.99m },                 new ProductWithNullablePrice { Name="F", Price= 3.99m },                 new ProductWithNullablePrice { Name="S", Price=null}             };         }         public override string ToString()         {             return string.Format("{0}: {1}", Name, Price);         }     }     class MainClass     {         static void Main()         {             List<ProductWithNullablePrice> products = ProductWithNullablePrice.GetSampleProducts();             foreach (ProductWithNullablePrice product in products.Where(p => p.Price == null))             {                 Console.WriteLine(product.Name);             }         }     }