private void btnThemHoaDon_Click(object sender, EventArgs e) { if (gioHangList.Count > 0) { var luaChonList = new List <LuaChonViewModel>(); gioHangList.ForEach(v => { if (v.MaTopping.Count == 1) { luaChonList.Add(new LuaChonViewModel(-1, v.MaSanPham, v.MaTopping.Last())); } else { v.MaTopping.ForEach(n => { luaChonList.Add(new LuaChonViewModel(-1, v.MaSanPham, n)); }); } }); // ma lua chon. var listId = requestData.PostLuaChon(luaChonList).ToList(); // call ham get lua chon tu` server de? lay' du lieu. loc theo ma lua chon. var _luaChonList = requestData.GetListLuaChon(); var luaChonListItem = new List <LuaChonViewModel>(); listId.ForEach(v => { luaChonListItem.AddRange(_luaChonList.Where(n => n.MaLuaChon == v)); }); if (listId != null) { //tao dictionary luu thong tin lua. chon var thongTinLuaChonList = new Dictionary <int, int>(); // so sanh voi gio hang de lay thong tin cua san pham de? them vao` hoa' don gioHangList.ForEach(v => { listId.ForEach(n => { if (luaChonListItem.Where(m => m.MaLuaChon == n && m.MaSanPham == v.MaSanPham).Count() == v.MaTopping.Count()) { thongTinLuaChonList.Add(n, v.SoLuong); } }); }); // tao bien thong tin hoa don de? thao tac ham` them vao hoa don ThongTinHoaDon thongTinHoaDon = new ThongTinHoaDon(thongTinLuaChonList, taiKhoan.Id, DateTime.Now, txtMoTa.Text); bool check = requestData.PostHoaDon(thongTinHoaDon); if (check == true) { MessageBox.Show("Tạo hóa đơn thành công!", "OK!!!", MessageBoxButtons.OK, MessageBoxIcon.Information); gioHangList.Clear(); this.Dispose(); } else { MessageBox.Show("Tạo hóa đơn thất bại!", "ERROR!!!", MessageBoxButtons.OK, MessageBoxIcon.Error); gioHangList.Clear(); this.Dispose(); } } else { MessageBox.Show("Thêm lựa chọn thất bại!"); } //listId.ForEach(v => //{ // MessageBox.Show(v.ToString()); //}); } else { MessageBox.Show("Chưa chọn sản phẩm để tạo hóa đơn!", "EEROR!!!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }