public static void Serialize(string Path, PotPeriodFiller[] Fillers) { var pots = Fillers.GroupBy(o => KeySelector(o.PotKey, o.PeriodKey)) .Select(o => PotXmlModel.LoadFromFillers(o.ToArray())) .ToList(); pots.RemoveAll(o => o == null); XmlSerializer serializer = GetSerializer(); using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate)) { serializer.Serialize(fs, pots); } }
public static PotXmlModel LoadFromFillers(PotPeriodFiller[] PPFillers) { PotXmlModel pot = new PotXmlModel(); int potKey = PPFillers[0].PotKey, periodKey = PPFillers[0].PeriodKey; foreach (var filler in PPFillers) { if (potKey != filler.PotKey || periodKey != filler.PeriodKey) throw new ArgumentException("Fillers should be from one PotPeriod"); } try { pot.PotName = Form1.DataContext.GetTable<PotObject>() .Where(o => o.PotKey == potKey) .ToArray()[0].PName; pot.PeriodTimeStamp = Form1.DataContext.GetTable<PeriodsObject>() .Where(o => o.PeriodKey == periodKey) .ToArray()[0].PTimeStamp; } catch { return null; } pot.Fillers = new FillerXmlModel[PPFillers.Length]; for (int i = 0; i < pot.Fillers.Length; i++) { pot.Fillers[i] = new FillerXmlModel(); pot.Fillers[i].FillerName = Form1.DataContext.GetTable<FillersObject>() .Where(o => o.FillerKey == PPFillers[i].FillerKey) .ToArray()[0].FName; pot.Fillers[i].FillerQuantity = PPFillers[i].Quantity; } return pot; }
public static PotPeriodFiller[] ToFillers(PotXmlModel model) { int pot = -1, period = -1; try { pot = Form1.DataContext.GetTable<PotObject>() .Where(o => o.PName == model.PotName) .ToArray()[0].PotKey; period = Form1.DataContext.GetTable<PeriodsObject>() .Where(o => o.PTimeStamp == model.PeriodTimeStamp) .ToArray()[0].PeriodKey; } catch { return null; } List<PotPeriodFiller> fillers = new List<PotPeriodFiller>(); foreach(var filler in model.Fillers) { int fkey = -1; try { fkey = Form1.DataContext.GetTable<FillersObject>() .Where(o => o.FName == filler.FillerName) .ToArray()[0].FillerKey; } catch { break; } PotPeriodFiller potFiller = new PotPeriodFiller(); potFiller.PotKey = pot; potFiller.PeriodKey = period; potFiller.FillerKey = fkey; potFiller.Quantity = filler.FillerQuantity; fillers.Add(potFiller); } return fillers.ToArray(); }
void RelationsLoadOptions() { if (Pot == null || Period == null) return; int pot = Pot.PotKey; int period = Period.PeriodKey; ResetControls(); // PotPeriod PotPeriodRelation = PotPeriod.Load(pot, period); if (PotPeriodRelation != null) { checkBoxNewCreated.Checked = !PotPeriodRelation.Availability; } else PotPeriodRelation = new PotPeriod() { PotKey = pot, PeriodKey = period }; // PotPeriodAvailable PotPeriodAvailableRelation = PotPeriodAvailable.Load(pot, period); if (PotPeriodAvailableRelation != null) { var method = MethodsObject.Load(PotPeriodAvailableRelation.MethodKey); if (method != null) ComboSelect(comboBoxFillingMethod, method.MName); var state = StatesObject.Load(PotPeriodAvailableRelation.StateKey); if (state != null) ComboSelect(comboBoxState, state.SName); checkBoxEmpty.Checked = PotPeriodAvailableRelation.Empty; numericMinutesSpent.Value = (decimal)PotPeriodAvailableRelation.MinutesSpent; } else PotPeriodAvailableRelation = new PotPeriodAvailable() { PotKey = pot, PeriodKey = period }; // Fillers PotPeriodWaterFiller = PotPeriodFiller.Load(pot, period, (int)_fillerWater.FillerKey); if (PotPeriodWaterFiller != null) { numericWaterCount.Value = (decimal)PotPeriodWaterFiller.Quantity; } else PotPeriodWaterFiller = new PotPeriodFiller() { PotKey = pot, PeriodKey = period, FillerKey = (int)_fillerWater.FillerKey }; PotPeriodHgFiller = PotPeriodFiller.Load(pot, period, (int)_fillerHG.FillerKey); if (PotPeriodHgFiller != null) { numericHgCount.Value = (decimal)PotPeriodHgFiller.Quantity; } else PotPeriodHgFiller = new PotPeriodFiller() { PotKey = pot, PeriodKey = period, FillerKey = (int)_fillerHG.FillerKey }; UpdatePreviewIni(); }