public static void checkIfNotificationIsNecessary(int projectId) { try { nova_ataEntities db = new nova_ataEntities(); string projectName = string.Empty; string businessUnit = string.Empty; bool isAllCompleted = true; bool needsNotification = false; var tasks = from t in db.tasks where t.criticalActivity == "5000" where t.projectId == projectId select new { t.status, t.projects.businessUnit, t.projects.name }; if (tasks.ToList().Count() > 0) { businessUnit = tasks.ToList()[0].businessUnit; projectName = tasks.ToList()[0].name; foreach (var t in tasks) { if (!t.status.ToUpper().Equals("COMPLETO")) { isAllCompleted = false; } } } else { isAllCompleted = false; } var notification = (dynamic)null; notification = db.notifications.Find(projectId); if (notification == null) { needsNotification = true; notification = db.notifications.Create(); notification.projectId = projectId; notification.status = "NOT SENT"; db.notifications.Add(notification); db.SaveChanges(); } else { if (notification.status.Equals("NOT SENT")) { needsNotification = true; } } if (isAllCompleted && needsNotification) { notification.status = "SENT"; db.SaveChanges(); Mail.sendMail(0, UserDAO.instanceByBusinessUnit(businessUnit), projectId, projectName); } if (!isAllCompleted && !needsNotification) { notification.status = "NOT SENT"; db.SaveChanges(); Mail.sendMail(1, UserDAO.instanceByBusinessUnit(businessUnit), projectId, projectName); } } catch (Exception ex) { throw new Exception(ex.Message); } }