private void UpdateDocumentParams(Document doc, IList<ParameterViewModel> parameters) { var names = _repParamNames.FindAll(d => d.Category == ParameterCategory.Document && d.DocType == doc.Type && d.IsFact == false).ToDictionary(k => k.Name, v => v); var docParams = _repDocParams.FindAll(d => d.DocumentId == doc.Id).ToDictionary(k => k.ParameterId, v => v); var list = new List<DocumentParameter>(); foreach (var prm in parameters) { var pn = names[prm.Name]; DocumentParameter docParam = null; if (docParams.ContainsKey(pn.Id)) { docParam = docParams[pn.Id]; docParam.DateValue = prm.DateValue; docParam.FloatValue = prm.FloatValue; docParam.IntValue = prm.MiscId; docParam.StringValue = prm.StringValue; } else { docParam = new DocumentParameter { ParameterId = pn.Id, DocumentId = doc.Id, DateValue = prm.DateValue, FloatValue = prm.FloatValue, IntValue = prm.MiscId, StringValue = prm.StringValue }; } list.Add(docParam); }; _repDocParams.AddRange(list.Where(p => p.Id == 0).AsEnumerable()); _repDocParams.UpdateRange(list.Where(p => p.Id != 0).AsEnumerable()); }
private DocumentParameter GetOrCreateDocumentParameter(bool shouldSave, Document doc, PrmFactName paramName, string strValue = null, int? intValue = null, float? floatValue = null, DateTime? dateValue = null) { strValue = strValue ?? string.Empty; var docPrm = _db.DocumentParameters.SingleOrDefault(d => d.DocumentId == doc.Id && d.ParameterId == paramName.Id); if (docPrm == null) { docPrm = new DocumentParameter() { DateValue = dateValue, IntValue = intValue, FloatValue = floatValue, StringValue = strValue, DocumentId = doc.Id, ParameterId = paramName.Id }; _db.DocumentParameters.Add(docPrm); } else { docPrm.DateValue = dateValue; docPrm.IntValue = intValue; docPrm.FloatValue = floatValue; docPrm.StringValue = strValue; docPrm.DocumentId = doc.Id; docPrm.ParameterId = paramName.Id; } if (shouldSave) { _db.SaveChanges(); } return docPrm; }
private DocumentParameter UpdateOrCreateDocumentParameter(Document doc, ParameterName paramName, string strValue = null, int? intValue = null, float? floatValue = null, DateTime? dateValue = null) { strValue = NormalizeString(strValue, true); var docPrm = _db.DocumentParameters.SingleOrDefault(d => d.DocumentId == doc.Id && d.ParameterId == paramName.Id); if (docPrm == null) { docPrm = new DocumentParameter() { DateValue = dateValue, IntValue = intValue, FloatValue = floatValue, StringValue = strValue, DocumentId = doc.Id, ParameterId = paramName.Id }; _db.DocumentParameters.Add(docPrm); } else { docPrm.DateValue = dateValue; docPrm.IntValue = intValue; docPrm.FloatValue = floatValue; docPrm.StringValue = strValue; docPrm.DocumentId = doc.Id; docPrm.ParameterId = paramName.Id; }; _db.SaveChanges(); return docPrm; }