public void AddWeight(string date, double newWeight) { DietEntities db = new DietEntities(); int nextRecNum = 0; var records = (from items in db.Weights select items); foreach (var item in records) { if (item.recNum > nextRecNum) { nextRecNum = item.recNum; } } nextRecNum++; Weight weight = new Weight(); weight.recNum = nextRecNum; weight.measureDate = date; weight.weight1 = (float)newWeight; try { db.Weights.Add(weight); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } db.Dispose(); }
public void AddNewFood(string foodName, int calories) { DietEntities db = new DietEntities(); int nextRecNum = 0; var records = (from items in db.Foods select items); foreach (var item in records) { if (item.recNum > nextRecNum) { nextRecNum = item.recNum; } } nextRecNum++; Food food = new Food(); food.recNum = nextRecNum; food.food1 = foodName; food.calories = calories; try { db.Foods.Add(food); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } db.Dispose(); }
public int GetNextRecNum(string table) { DietEntities db = new DietEntities(); int highest = 0; switch (table) { case "Food": { var records = (from items in db.Foods group items by true into r select new { max = r.Max(z => z.recNum) }); foreach (var r in records) { highest = r.max; } break; } case "Daily": { var records = (from items in db.Dailies group items by true into r select new { max = r.Max(z => z.recNum) }); foreach (var r in records) { highest = (int)r.max; } break; } case "Weight": { var records = (from items in db.Weights group items by true into r select new { max = r.Max(z => z.recNum) }); foreach (var r in records) { highest = r.max; } break; } default: break; } db.Dispose(); return(highest + 1); }
public void _timerElapsed(object sender, ElapsedEventArgs e) { DietEntities db = new DietEntities(); int count = (from item in db.Foods where item.calories > 0 select item).Count(); db.Dispose(); }
private int GetFoodIndex(string food) { DietEntities db = new DietEntities(); int item = (from items in db.Foods where items.food1 == food select items.recNum).FirstOrDefault(); db.Dispose(); return(item); }
public int GetFoodCalories(int id) { // TODO: retrieve the real product info from DB using EF DietEntities db = new DietEntities(); int item = (from items in db.Foods where items.recNum == id select items.calories).FirstOrDefault().Value; db.Dispose(); return(item); }
public string GetFoodName(int id) { // TODO: retrieve the real product info from DB using EF DietEntities db = new DietEntities(); string item = (from items in db.Foods where items.recNum == id select items.food1).First(); db.Dispose(); return(item); }
public void AddPersonalData(string name, float height, float iWeight, float tWeight, string ssid) { DietEntities db = new DietEntities(); User pd = new User(); pd.name = name; pd.height = height; pd.initialWeight = iWeight; pd.targetWeight = tWeight; pd.SSID = ssid; try { db.Users.Add(pd); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } db.Dispose(); }
public void AddDailyFood(string foodName, double qty) { DietEntities db = new DietEntities(); DailyMeal dailyMeal = new DailyMeal(); DateTime date; date = DateTime.Today.Date; string currentDate; string yesterday; bool sameday; double dailyTotal; int lastRecNum; DateTime now = DateTime.Now; currentDate = now.ToString("MM/dd/yyyy"); yesterday = now.AddDays(-1).ToString("MM/dd/yyyy"); int foodIndex = GetFoodIndex(foodName); sameday = db.DailyMeals.Any(entry => entry.dayDate.Equals(date)); // if any record's column 'daydate' equals 'date' from this method's arguement list // 'entry' is a local LINQ arguement referring to a record in the table // see https://www.dotnetperls.com/any lastRecNum = db.WeeklyCalories.Count(); if (!sameday) { // total all calories for week and enter in WeeklyCalories if (now.ToString("dddd") == "Monday") { dailyTotal = SumDailyCalories(); WeeklyCalory wc = new WeeklyCalory(); wc.recNum = lastRecNum + 1; wc.weekEnding = yesterday; wc.totalCalories = (float)dailyTotal; try { db.WeeklyCalories.Add(wc); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } // clear DailyMeals // Alternate Method // ************************************************** //string connstr = "Provider=SQLOLEDB;" // + "Data Source = .\\SQLEXPRESS;" // + "Initial Catalog =Diet;" // + "Integrated Security=SSPI"; //DataContext context = new DataContext(connstr); //context.ExecuteCommand("TRUNCATE TABLE DailyMeals"); // ************************************************** var allRecords = (from item in db.DailyMeals select item); foreach (DailyMeal item in allRecords) { db.DailyMeals.Remove(item); } try { db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } lastRecNum = 0; } } // now add the latest entry dailyMeal.recNum = lastRecNum + 1; dailyMeal.dayDate = date; dailyMeal.food = foodIndex; dailyMeal.quantity = (float)qty; try { db.DailyMeals.Add(dailyMeal); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } db.Dispose(); }
public void AddDailyFoodItem(string foodName, double qty) { DietEntities db = new DietEntities(); Daily daily = new Daily(); DateTime date; date = DateTime.Today.Date; int foodIndex = GetFoodIndex(foodName); //string today = now.ToString("MM/dd/yyyy"); int nextRecNum = 0; //var count = (from item in db.Dailies // select item); //foreach (var item in count) //{ // if (item.recNum > nextRecNum) // nextRecNum = (int)item.recNum; //} nextRecNum = int.Parse(db.Dailies .OrderByDescending(p => p.recNum) .Select(r => r.recNum) .First().ToString()); nextRecNum++; daily.day_of_week = date; daily.recNum = nextRecNum; daily.food = foodIndex; daily.qty = (float)qty; //var rec = (from item in db.Dailies // where item.day_of_week == sqlDate // select item); //foreach (var item in rec) //{ // daily.recNum = item.recNum; // daily.day_of_week = item.day_of_week; // daily = item; // daily.food = foodIndex; // daily.qty = (float)qty; // break; //} //foreach (var item in rec) //{ // daily.recNum = item.recNum; // daily.day_of_week = item.day_of_week; // daily = item; // if (!item.food1.HasValue) // { // daily.food1 = foodIndex; // daily.qty1 = (float)qty; // break; // } // if (!item.food2.HasValue) // { // daily.food2 = foodIndex; // daily.qty2 = (float)qty; // break; // } // if (!item.food3.HasValue) // { // daily.food3 = foodIndex; // daily.qty3 = (float)qty; // break; // } // if (!item.food4.HasValue) // { // daily.food4 = foodIndex; // daily.qty4 = (float)qty; // break; // } // if (!item.food5.HasValue) // { // daily.food5 = foodIndex; // daily.qty5 = (float)qty; // break; // } // if (!item.food6.HasValue) // { // daily.food6 = foodIndex; // daily.qty6 = (float)qty; // break; // } // if (!item.food7.HasValue) // { // daily.food7 = foodIndex; // daily.qty7 = (float)qty; // break; // } // if (!item.food8.HasValue) // { // daily.food8 = foodIndex; // daily.qty8 = (float)qty; // break; // } // if (!item.food9.HasValue) // { // daily.food9 = foodIndex; // daily.qty9 = (float)qty; // break; // } // if (!item.food10.HasValue) // { // daily.food10 = foodIndex; // daily.qty10 = (float)qty; // break; // } //} try { db.Dailies.Add(daily); db.SaveChanges(); } catch (Exception e) { Debug.WriteLine(e.InnerException); } //var original = db.Dailies.Find(daily.recNum); //if (original != null) //{ // db.Entry(original).CurrentValues.SetValues(daily); // db.SaveChanges(); //} //else //{ // daily = new Daily(); // daily.recNum = nextRecNum; // daily.day_of_week = DateTime.Parse(date).Date; // daily.food = foodIndex; // daily.qty = (float)qty; // try // { // db.Dailies.Add(daily); // db.SaveChanges(); // } // catch (Exception e) // { // Debug.WriteLine(e.InnerException); // } //} db.Dispose(); AddDailyFood(foodName, qty); }
public double GetDailyTotal(string date) { DietEntities db = new DietEntities(); double total = 0; DateTime sqlDate = new DateTime(); sqlDate = DateTime.Parse(date); var rec = (from item in db.Dailies where item.day_of_week == sqlDate select item); foreach (var item in rec) { total += (Convert.ToDouble(GetFoodCalories((int)item.food)) * (double)item.qty); } //foreach (var item in rec) //{ // if (item.food1.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food1.Value)) * item.qty1.Value); // } // else // { // break; // } // if (item.food2.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food2.Value)) * item.qty2.Value); // } // else // { // break; // } // if (item.food3.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food3.Value)) * item.qty3.Value); // } // else // { // break; // } // if (item.food4.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food4.Value)) * item.qty4.Value); // } // else // { // break; // } // if (item.food5.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food5.Value)) * item.qty5.Value); // } // else // { // break; // } // if (item.food6.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food6.Value)) * item.qty6.Value); // } // else // { // break; // } // if (item.food7.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food7.Value)) * item.qty7.Value); // } // else // { // break; // } // if (item.food8.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food8.Value)) * item.qty8.Value); // } // else // { // break; // } // if (item.food9.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food9.Value)) * item.qty9.Value); // } // else // { // break; // } // if (item.food10.HasValue) // { // total += (Convert.ToDouble(GetFoodCalories(item.food10.Value)) * item.qty10.Value); // } // else // { // break; // } //} db.Dispose(); return(total); }