Which products are currently out of stock - CSharp LINQ

CSharp examples for LINQ:where

Description

Which products are currently out of stock

Demo Code




using System;/*from w  w  w  .j a va2  s  .  c o  m*/
using System.Collections.Generic;
using System.Linq;
using System.IO;
class Program
{
    static void Main(string[] args)
    {
        var products = new List<Product> {
        new Product { Name = "MongoDB", OnHand = 0 },
        new Product { Name = "Java", OnHand = 56 },
        new Product { Name = "XML", OnHand = 300 },
        new Product { Name = "MySQL", OnHand = 0 },
        new Product { Name = "Whatsit", OnHand = 1 },
      };
        Console.WriteLine("\tProducts currently out of stock:");
        var productsOutOfStock = from p in products
                                 where p.OnHand < 1
                                 select p;
        foreach (var prod in productsOutOfStock)
        {
            Console.WriteLine("\t\t{0}: {1} on hand", prod.Name, prod.OnHand);
        }
    }
}
class Product
{
    public string Name;
    public int OnHand;
    public decimal UnitCost;
}

Related Tutorials