[Linq雜記] C# 使用 Lambda Foreach

Lambda Foreach 的使用筆記

測試資料
    class GoodsInfo
    {
        public int GoodsId { get; set; } 
        public string GoodsName { get; set; } 
        public int Price { get; set; } 
    }
    class Order
    {
        public int OrderId { get; set; }
        public int GoodsId { get; set; }
        public int Quantity { get; set; }
    }
    class SeedData
    {
        public SeedData()
        {
            this.GenerateGoodsInfos = new List<GoodsInfo>();
            string JsonGoodsInfos 
                = "[{\"GoodsId\":0,\"GoodsName\":\"紅茶\",\"Price\":25},"
                + "{\"GoodsId\":1,\"GoodsName\":\"綠茶\",\"Price\":30},"
                + "{\"GoodsId\":2,\"GoodsName\":\"奶茶\",\"Price\":40},]";
            GenerateGoodsInfos 
                =JsonConvert.DeserializeObject<List<GoodsInfo>>(JsonGoodsInfos);

            this.GenerateOrders = new List<Order>();
            string JsonOrders
                = "[{\"OrderId\":0,\"GoodsId\":2,\"Quantity\":3},"
                + "{\"OrderId\":1,\"GoodsId\":1,\"Quantity\":5},"
                + "{\"OrderId\":2,\"GoodsId\":0,\"Quantity\":7}]";
            GenerateOrders
                = JsonConvert.DeserializeObject<List<Order>>(JsonOrders);
        }

        public List<GoodsInfo> GenerateGoodsInfos { get; set; }
        public List<Order> GenerateOrders { get; set; }
    }
    class OrderViewModel: Order
    {
        public string GoodsName { get; set; }
        public int? Price { get; set; }
    }
正片開始
    class Program
    {
        static void Main(string[] args)
        {
            SeedData seedData = new SeedData();

            #region 取得ViewModel資料
            List<OrderViewModel> viewModel = new List<OrderViewModel>();
            var config = new MapperConfiguration(c => {
                c.CreateMap<Order, OrderViewModel>();
            });
            IMapper mapper = config.CreateMapper();
            viewModel = mapper.Map<List<Order>, List<OrderViewModel>>(seedData.GenerateOrders);
            #endregion

            #region 第一種方式
            Console.WriteLine("\n第一種方式");
            foreach (var iOrder in viewModel)
            {
                foreach (var iGoodsInfo in seedData.GenerateGoodsInfos)
                {
                    if (iOrder.GoodsId == iGoodsInfo.GoodsId)
                    {
                        iOrder.GoodsName = iGoodsInfo.GoodsName;
                        iOrder.Price = iGoodsInfo.Price;
                    }
                }

                Console.WriteLine(JsonConvert.SerializeObject(iOrder));
            }
            #endregion

            #region 第二種方式
            Console.WriteLine("\n第二種方式");
            foreach (var oitem in viewModel)
            {
                var GoodsInfo = seedData.GenerateGoodsInfos.Where(x =>
                      x.GoodsId == oitem.GoodsId
                ).FirstOrDefault();

                oitem.GoodsName = GoodsInfo.GoodsName;
                oitem.Price = GoodsInfo.Price;

                Console.WriteLine(JsonConvert.SerializeObject(oitem));
            }
            #endregion

            #region 第三種方式
            Console.WriteLine("\n第三種方式");
            viewModel.ForEach(vm => {
                var GoodsInfo = seedData.GenerateGoodsInfos.Where(x =>
                      x.GoodsId == vm.GoodsId
                ).FirstOrDefault();

                vm.GoodsName = GoodsInfo.GoodsName;
                vm.Price = GoodsInfo.Price;
                Console.WriteLine(JsonConvert.SerializeObject(vm));
            });
            #endregion

            Console.ReadKey();
        }
    }
輸出結果