public List<OrderLine> getOrder(int id)
 {
     Debug.WriteLine("OrdreID: " + id);
     var db = new DatabaseContext();
     var lines = db.OrderLines.Include(p => p.Products).Where(o => o.OrdersId == id).ToList();
     Debug.WriteLine("Antall ordrelinjer fra db");
     Debug.WriteLine(lines.Count);
     List<OrderLine> list = new List<OrderLine>();
     foreach (var item in lines)
     {
         var itemProduct = new Product()
         {
             itemnumber = item.Products.Id,
             name = item.Products.Name,
             price = item.Products.Price,
             description = item.Products.Description
         };
         list.Add(new OrderLine()
         {
             product = itemProduct,
             quantity = item.Quantity
         });
     }
     return list;
 }
        public List<Product> getAll(int? id, String sc, int? sort)
        {
           
            var db = new DatabaseContext();
            List<Product> allProducts = new List<Product>();
            var products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Name).ToList();
            switch (sort)
            {
                case 1:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Name).ToList();
                    break;
                case 2:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderBy(p => p.Price).ToList();
                    break;
                case 3:
                    products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.SubCategoriesId == id).OrderByDescending(p => p.Price).ToList();
                    break;
            }
           
            foreach (var p in products)
            {

                var product = new Product()
                {
                    itemnumber = p.Id,
                    name = p.Name,
                    description = p.Description,
                    price = p.Price,
                    volum = p.Volum,
                    producer = p.Producers.Name,
                    pricePerLitre = pricePerLitre(p.Price, p.Volum),
                    category = p.SubCategories.Categories.Name,
                    categoryid = p.SubCategories.Categories.Id,
                    subCategory = p.SubCategories.Name,
                    subCategoryid = p.SubCategories.Id,
                    country = p.Countries.Name

                };
                allProducts.Add(product);
            }

            return allProducts;
        }
 public ShoppingCartItem(Product p, int qty)
 {
     product = p;
     quantity = qty;
     price = qty * product.price;
 }
        public List<Product> getMostSold()
        {
            var db = new DatabaseContext();
            List<int> topOrders = db.OrderLines
                                    .GroupBy(p => p.ProductsId)
                                    .OrderByDescending(gp => gp.Count())
                                    .Take(4)
                                    .Select(g => g.Key).ToList();
            List<Products> products = new List<Products>(); 
            
            foreach (var i in topOrders)
            {
                products.Add(db.Products.Where(p => p.Id == i).First());
            }
            List<Product> prod = new List<Product>();
            foreach (var p in products)
            { 
                 var product = new Product()
                {
                    itemnumber = p.Id,
                    name = p.Name,
                    description = p.Description,
                    price = p.Price,
                    volum = p.Volum,
                    producer = p.Producers.Name,
                    subCategory = p.SubCategories.Name,
                    subCategoryid = p.SubCategories.Id,
                    country = p.Countries.Name
                };
                prod.Add(product);
            }

            
            return prod;
        }
 public List<Product> getResult(string searchString)
 {
     var db = new DatabaseContext();
     List<Product> foundProducts = new List<Product>();
     var products = db.Products.Include(p => p.SubCategories.Categories).Where(p => p.Name.ToUpper().Contains(searchString.ToUpper())
                     || p.Description.ToUpper().Contains(searchString.ToUpper())).ToList();
     foreach (var p in products)
     {
         var product = new Product()
         {
             itemnumber = p.Id,
             name = p.Name,
             description = p.Description,
             price = p.Price,
             volum = p.Volum,
             producer = p.Producers.Name,
             pricePerLitre = pricePerLitre(p.Price, p.Volum),
             category = p.SubCategories.Categories.Name,
             categoryid = p.SubCategories.Categories.Id,
             subCategory = p.SubCategories.Name,
             subCategoryid = p.SubCategories.Id,
             country = p.Countries.Name
         };
         foundProducts.Add(product);
     }
     return foundProducts;
 }