//delete the insttype you choose private void delete2_Click(object sender, EventArgs e) { try { InstType inst = new InstType(); inst.Id = Convert.ToInt16(id2.SelectedItem); inst.TypeName = insttype2.Text; inst.Underlying = underlying2.Text; var v = from i in portfolio.Instruments where i.InstTypeId == inst.Id select i; foreach (Instrument i in v)//if you want to delete a insttype, need delete all the instrumets, price and trade relate to the insttype { var v2 = from j in portfolio.Trades where j.InstrumentId == i.Id select j;//first delete the trade foreach (Trade j in v2) { Trade dele = portfolio.Trades.Single(k => k.Id == j.Id); portfolio.Trades.Remove(dele); } var v3 = from l in portfolio.Prices where l.InstrumentId == i.Id select l;//second delte the price foreach (Price m in v3) { Price dele2 = portfolio.Prices.Single(n => n.Id == m.Id); portfolio.Prices.Remove(dele2); } Instrument dele3 = portfolio.Instruments.Single(p => p.Id == i.Id); portfolio.Instruments.Remove(dele3);//then delete the instrument } InstType delete = portfolio.InstTypes.Single(i => i.Id == inst.Id); portfolio.InstTypes.Remove(delete);//finally delete the insttype portfolio.SaveChanges(); if (inst.TypeName.ToUpper() == "BARRIEROPTION")//if you want to delete barrieroption, need to delte in barrieroption taple { var v1 = from i in portfolio.BarrierOptions where i.InstTypeId == inst.Id select i; int id = v1.First().Id; BarrierOption delete1 = portfolio.BarrierOptions.Single(i => i.Id == id); portfolio.BarrierOptions.Remove(delete1); portfolio.SaveChanges(); } else if (inst.TypeName.ToUpper() == "DIGITALOPTION")//if you want to delete digitaloption, need to delte in digitaloption taple { var v2 = from i in portfolio.DigitalOptions where i.InstTypeId == inst.Id select i; int id = v2.First().Id; DigitalOption delete2 = portfolio.DigitalOptions.Single(i => i.Id == id); portfolio.DigitalOptions.Remove(delete2); portfolio.SaveChanges(); } else if (inst.TypeName.ToUpper() == "LOOKBACKOPTION")//if you want to delete lookbackoption, need to delte in lookbackoption taple { var v3 = from i in portfolio.LookBackOptions where i.InstTypeId == inst.Id select i; int id = v3.First().Id; LookBackOption delete3 = portfolio.LookBackOptions.Single(i => i.Id == id); portfolio.LookBackOptions.Remove(delete3); portfolio.SaveChanges(); } MessageBox.Show("Delete data successfully", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } catch { MessageBox.Show("Something wrong, please check wheather the inputs are correct."); } }
private void add2_Click(object sender, EventArgs e) { try { InstType inst = new InstType(); if (insttype2.Text != "" && underlying2.Text != "") { inst.TypeName = insttype2.Text.ToString(); inst.Underlying = underlying2.Text.ToString(); portfolio.InstTypes.Add(inst); portfolio.SaveChanges(); IQueryable<InstType> var = from p in portfolio.InstTypes orderby p.Id descending select p; inst.Id = var.First().Id;//find the latest insttype you add if (inst.TypeName.ToUpper() == "BARRIEROPION")//if you add barrieroption, you should also add wheather it is in out up down and its barrierprice { barrier2.ReadOnly = false; up_in2.Enabled = true; up_out2.Enabled = true; down_in2.Enabled = true; down_out2.Enabled = true; BarrierOption bar = new BarrierOption(); bar.barrier = Convert.ToDouble(barrier2.Value); bar.InstTypeId = inst.Id; if (up_in2.Checked == true) { bar.IsIn = true; bar.IsUp = true; } else if (down_in2.Checked == true) { bar.IsIn = true; bar.IsUp = false; } else if (up_out2.Checked == true) { bar.IsIn = false; bar.IsUp = true; } else if (down_out2.Checked == true) { bar.IsIn = false; bar.IsUp = false; } portfolio.BarrierOptions.Add(bar); portfolio.SaveChanges(); } else if (insttype2.Text.ToUpper() == "DIGITALOPTION")//if you add digitaloption, you should also add its rebate price { rebate2.ReadOnly = false; DigitalOption dig = new DigitalOption(); dig.rebate = Convert.ToDouble(rebate2.Value); dig.InstTypeId = inst.Id; portfolio.DigitalOptions.Add(dig); portfolio.SaveChanges(); } else if (insttype2.Text.ToUpper() == "LOOKBACKOPTION")//if you add lookbackoption, you should choose fixing or floating for payoff function { fix2.Enabled = true; floating2.Enabled = true; LookBackOption look = new LookBackOption(); if (fix2.Checked == true) look.IsFixed = true; else look.IsFixed = false; look.InstTypeId = inst.Id; portfolio.LookBackOptions.Add(look); portfolio.SaveChanges(); } MessageBox.Show("Data added successfully", "Notice"); this.Close(); } else MessageBox.Show("Please input the TypeName and Underlying", "Notice"); } catch { MessageBox.Show("Something wrong, please check wheather the inputs are correct."); } }
private void testdata() { InstType eur = new InstType(); eur.TypeName = "EuropeanOption"; eur.Underlying = "MSFT"; portfolio.InstTypes.Add(eur); portfolio.SaveChanges(); InstType asi = new InstType(); asi.TypeName = "AsianOption"; asi.Underlying = "GOOG"; portfolio.InstTypes.Add(asi); portfolio.SaveChanges(); InstType bar = new InstType(); bar.TypeName = "BarrierOption"; bar.Underlying = "SINA"; portfolio.InstTypes.Add(bar); portfolio.SaveChanges(); var v1 = (from i in portfolio.InstTypes where i.TypeName == "BarrierOption" select i.Id).First(); BarrierOption b = new BarrierOption(); b.InstTypeId = v1; b.IsIn = true; b.IsUp = true; b.barrier = 60; portfolio.BarrierOptions.Add(b); portfolio.SaveChanges(); InstType dig = new InstType(); dig.TypeName = "DigitalOption"; dig.Underlying = "WEBO"; portfolio.InstTypes.Add(dig); portfolio.SaveChanges(); var v2 = (from i in portfolio.InstTypes where i.TypeName == "DigitalOption" select i.Id).First(); DigitalOption d = new DigitalOption(); d.InstTypeId = v2; d.rebate = 50; portfolio.DigitalOptions.Add(d); portfolio.SaveChanges(); InstType look = new InstType(); look.TypeName = "LookBackOption"; look.Underlying = "BAIDU"; portfolio.InstTypes.Add(look); portfolio.SaveChanges(); var v3 = (from i in portfolio.InstTypes where i.TypeName == "LookBackOption" select i.Id).First(); LookBackOption l = new LookBackOption(); l.InstTypeId = v3; l.IsFixed = true; portfolio.LookBackOptions.Add(l); portfolio.SaveChanges(); InstType ran = new InstType(); ran.TypeName = "RangeOption"; ran.Underlying = "SOHU"; portfolio.InstTypes.Add(ran); portfolio.SaveChanges(); InstType sto = new InstType(); sto.TypeName = "Stock"; sto.Underlying = "MSFT"; portfolio.InstTypes.Add(sto); portfolio.SaveChanges(); }