Advertisement
Furai

Untitled

May 22nd, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. public void saveData()
  2.         {
  3.             try
  4.             {
  5.                 using (var context = new CarDealerDataContext())
  6.                 {
  7.                     foreach (var c in customers)
  8.                     {
  9.                         var _c = new Persistent.DB.Customer();
  10.                         bool flag = false;
  11.                         if (c.Id == 0)
  12.                         {
  13.                             flag = true;
  14.                             _c.Address = c.Address;
  15.                             _c.Name = c.Name;
  16.                             _c.Phone = c.Phone.ToString();
  17.                             if (c is Domain.Customers.Private)
  18.                             {
  19.                                 var p = (Domain.Customers.Private)c;
  20.                                 var _p = new Persistent.DB.Private();
  21.                                 _p.Age = p.Age;
  22.                                 _p.Sex = p.Sex.ToString();
  23.                                 _p.Customer = _c;
  24.                             }
  25.                             else if (c is Domain.Customers.Business)
  26.                             {
  27.                                 var b = (Domain.Customers.Business)c;
  28.                                 var _b = new Persistent.DB.Business();
  29.                                 _b.CompanyName = b.CompanyName;
  30.                                 _b.Fax = b.Fax;
  31.                                 _b.SerialNumber = b.SerialNumber;
  32.                                 _b.Customer = _c;
  33.                             }
  34.                         }
  35.                         else
  36.                         {
  37.                             _c = context.Customers.Single(_cu => _cu.CustomerId == c.Id);
  38.                         }
  39.                         if (c.Vehicles != null)
  40.                         {
  41.                             foreach (var v in c.Vehicles)
  42.                             {
  43.                                 if (v.Id == 0)
  44.                                 {
  45.                                     var _v = new Persistent.DB.Vehicle();
  46.                                     _v.Brand = v.Brand;
  47.                                     _v.Color = v.Color;
  48.                                     _v.Model = v.Model;
  49.                                     _v.Price = v.Price;
  50.                                     _v.State = v.CarState.ToString();
  51.                                     _v.Type = v.Type.ToString();
  52.                                     _v.Customer = _c;
  53.                                 }
  54.                             }
  55.                         }
  56.                         if (flag)
  57.                         {
  58.                             context.Customers.InsertOnSubmit(_c);
  59.                         }
  60.                     }
  61.                     foreach (var v in vehicles)
  62.                     {
  63.                         if (!context.Vehicles.Any(_v => _v.VehicleId == v.Id))
  64.                         {
  65.                             var _v = new Persistent.DB.Vehicle();
  66.                             _v.Brand = v.Brand;
  67.                             _v.Color = v.Color;
  68.                             _v.Model = v.Model;
  69.                             _v.Price = v.Price;
  70.                             _v.State = CarStates.Commision.ToString();
  71.                             _v.Type = v.Type.ToString();
  72.                             context.Vehicles.InsertOnSubmit(_v);
  73.                         }
  74.                     }
  75.                     context.SubmitChanges();
  76.                 }
  77.             }
  78.             catch
  79.             {
  80.                 // hahaha I've screwed sth up :((
  81.             }
  82.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement