TQC+ C# 認證教學手冊

308、產品庫存

☐易 ☑中 ☐難

參考程式碼

檔案:Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSD03
{
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<ProductDataItem> productDataItems = ProductData.GetData();
            IEnumerable<StockDataItem> stockDataItems = StockData.GetData();

            // TODO: write LINQ statements and output result.
            // TODO: must use Lambda Expression to handle condition.

            var query = from p in productDataItems
                        join s in stockDataItems
                        on p.ProductID equals s.ProductID
                        select new
                        {
                            id = p.ProductID,
                            cost = p.Cost,
                            stock = s.Stock,
                            total = p.Cost * s.Stock
                        };

            var query2 = query.Where(row => (row.cost * row.stock) >= 250000);

            foreach (var row in query2)
            {
                Console.WriteLine("Product id: {0}, cost: {1}, stock: {2}, total: ${3:N0}",
                    row.id, row.cost, row.stock, row.total);
            }

            Console.ReadLine();
        }
    }
}