Display Products With Unknown Price in CSharp

Description

The following code shows how to display Products With Unknown Price.

Example


     /*w  w w . j  a  v  a 2 s  . com*/
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);
            }
        }
    }

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ