示例#1
0
 public List <Food> GetAllFoods()
 {
     using (FoodDBEntities foodEntites = new FoodDBEntities())
     {
         return(foodEntites.Foods.ToList());
     }
 }
示例#2
0
 public List <Food> GetFoodsByMinCalories(int Calories)
 {
     using (FoodDBEntities foodEntites = new FoodDBEntities())
     {
         return(foodEntites.Foods.Where(f => f.Calories > Calories).ToList());
     }
 }
示例#3
0
 public List <Food> GetFoodsByName(string name)
 {
     using (FoodDBEntities foodEntites = new FoodDBEntities())
     {
         return(foodEntites.Foods.Where(f => f.Name.ToUpper().Contains(name.ToUpper())).ToList());
     }
 }
示例#4
0
 public Food GetFoodById(int id)
 {
     using (FoodDBEntities foodEntites = new FoodDBEntities())
     {
         return(foodEntites.Foods.FirstOrDefault(f => f.ID == id));
     }
 }
示例#5
0
 public void AddFood(Food food)
 {
     using (FoodDBEntities foodEntites = new FoodDBEntities())
     {
         foodEntites.Foods.Add(food);
         foodEntites.SaveChanges();
     }
 }
示例#6
0
        public Food DeleteFood(int id)
        {
            Food Delete;

            using (FoodDBEntities foodEntites = new FoodDBEntities())
            {
                Delete = foodEntites.Foods.FirstOrDefault(f => f.ID == id);
                foodEntites.Foods.Remove(Delete);
                foodEntites.SaveChanges();
            }
            return(Delete);
        }
示例#7
0
        public Food UpdateFood(int id, Food food)
        {
            Food Update = new Food();

            using (FoodDBEntities foodEntites = new FoodDBEntities())
            {
                Update             = foodEntites.Foods.FirstOrDefault(f => f.ID == id);
                Update.Name        = food.Name;
                Update.Calories    = food.Calories;
                Update.Ingridients = food.Ingridients;
                Update.Grade       = food.Grade;
                foodEntites.SaveChanges();
            }
            return(Update);
        }
示例#8
0
        public List <Food> GetBySearch(string name, int maxcal, int mincal, string ingridients, int grade)
        {
            using (FoodDBEntities foodDBEntities = new FoodDBEntities())
            {
                if (name == null && mincal.Equals(null) && maxcal.Equals(null) && ingridients == null && !grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Grade == grade).ToList());
                }

                if (name == null && mincal.Equals(null) && maxcal.Equals(null) && !(ingridients.Equals(null)) && grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Ingridients.ToUpper().Contains(ingridients.ToUpper())).ToList());
                }

                if (name == null && mincal > 0 && maxcal.Equals(null) && ingridients == null && grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Calories >= mincal).ToList());
                }

                if (name == null && mincal.Equals(null) && maxcal > 0 && ingridients == null && grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Calories <= maxcal).ToList());
                }

                if (!(name.Equals(null)) && mincal.Equals(null) && maxcal.Equals(null) && ingridients == null && grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Name.ToUpper().Contains(name.ToUpper())).ToList());
                }

                if (name == null && mincal > 0 && maxcal > 0 && ingridients == null && grade.Equals(null))
                {
                    return(foodDBEntities.Foods.Where(m => m.Calories >= mincal && m.Calories <= maxcal).ToList());
                }

                return(foodDBEntities.Foods.Where(m => m.Name.ToUpper().Contains(name.ToUpper()) || (!(ingridients.Equals(null)) && m.Ingridients.ToUpper().Contains(ingridients.ToUpper())) || (!grade.Equals(null)) && m.Grade == grade || (mincal > 0 && maxcal.Equals(null) && m.Calories >= mincal) || (mincal.Equals(null) && maxcal > 0 && m.Calories <= maxcal) || (mincal > 0 && maxcal > 0 && m.Calories >= mincal && m.Calories <= maxcal)).ToList());
            }
        }