public bool UpdateStatus(MNRequestMain o, params System.Linq.Expressions.Expression <Func <MNRequestMain, object> >[] properties) { var item = db.MNRequestMains.Where(i => i.ID == o.ID || i.OrderCode == o.OrderCode).SingleOrDefault(); if (item != null) { foreach (var propertie in properties) { var lambda = (LambdaExpression)propertie; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { memberExpression = (MemberExpression)((UnaryExpression)lambda.Body).Operand; } else { memberExpression = (MemberExpression)lambda.Body; } string propertyName = memberExpression.Member.Name; item.GetType().GetProperty(propertyName).SetValue(item, o.GetType().GetProperty(propertyName).GetValue(o)); } db.SaveChanges(); } else { return(false); } return(true); }
public bool UpdateItem(MNRequestMain o, Boolean isSaveDraft, Boolean isReturned, WFMainDetail w, params System.Linq.Expressions.Expression <Func <MNRequestMain, object> >[] properties) { if (o.Description == null) { o.Description = ""; } if (o.Reason == null) { o.Reason = ""; } using (TransactionScope transaction = new TransactionScope()) { try { MNRequestMain item = db.MNRequestMains.Where(i => i.ID == o.ID || i.OrderCode == o.OrderCode).SingleOrDefault(); if (item != null) { foreach (var propertie in properties) { var lambda = (LambdaExpression)propertie; MemberExpression memberExpression; if (lambda.Body is UnaryExpression) { memberExpression = (MemberExpression)((UnaryExpression)lambda.Body).Operand; } else { memberExpression = (MemberExpression)lambda.Body; } string propertyName = memberExpression.Member.Name; item.GetType().GetProperty(propertyName).SetValue(item, o.GetType().GetProperty(propertyName).GetValue(o)); } if (!isSaveDraft) { item.Status = (int)OrderStatus.SENDING; } } List <MNRequestMainDetail> lstRequestDetail = db.MNRequestMainDetails.Where(i => i.RequestMainID == item.ID).ToList(); if (lstRequestDetail.Count > 0) { foreach (MNRequestMainDetail i in lstRequestDetail) { db.MNRequestMainDetails.Remove(i); } } if (o.MNRequestMainDetails != null) { int sequence = 0; foreach (MNRequestMainDetail i in o.MNRequestMainDetails) { sequence += 1; i.DetailID = Guid.NewGuid().ToString(); i.RequestMainID = o.ID; i.ItemNo = Convert.ToInt16(GetItemNo(sequence)); db.MNRequestMainDetails.Add(i); } } if (!isSaveDraft) { if (isReturned) { WFMainDetail wfDetail = db.WFMainDetails.Where(i => i.WFMain.OrderCode == o.OrderCode && i.isFinished == 0 && i.NodeID == "").SingleOrDefault(); if (wfDetail != null) { wfDetail.isFinished = 1; wfDetail.CheckDate = DateTime.Now; } } db.WFMainDetails.Add(w); } db.SaveChanges(); transaction.Complete(); return(true); } catch { transaction.Dispose(); return(false); } } }