public void Update(Car car)
        {
            car.EnsureValid();

            DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, car);
            DataSource.DataContext.SubmitChanges();
        }
        public void Add(Car car)
        {
            if (car.User == null)
                car.User = User;

            car.EnsureValid();

            DataStore.Cars.Add(car);
        }
        public void Add(Car car)
        {
            if (car.User == null)
                car.User = User;

            car.EnsureValid();

            DataSource.DataContext.Cars.InsertOnSubmit(car);
            DataSource.DataContext.SubmitChanges();
        }
 public decimal CalculateAverageCostsPerKilometer(Car car)
 {
     try
     {
         return CalculateTotalCosts(car) / CalculateTotalKilometers(car);
     }
     catch {
         return 0;
     }
 }
        public decimal CalculateTotalCosts(Car car)
        {
            List<Refuelling> refuellings = RefuellingRepository.List(car);
            refuellings.Reverse();

            decimal returnValue = 0;
            returnValue += car.PurchasePrice;

            foreach (var refuelling in refuellings)
            {
                returnValue += refuelling.Total;
            }

            return returnValue;
        }
        public decimal CalculateAverageUsage(Car car)
        {
            List<Refuelling> refuellings = RefuellingRepository.List(car);

            decimal returnValue = 0;
            int count = 0;
            foreach (var refuelling in refuellings)
            {
                if (refuelling.Usage > 0)
                {
                    returnValue += refuelling.Usage;
                    count++;
                }
            }
            if (count > 0)
            {
                returnValue = returnValue / count;
            }

            return returnValue;
        }
		private void detach_Cars(Car entity)
		{
			this.SendPropertyChanging();
			entity.User = null;
		}
		private void attach_Cars(Car entity)
		{
			this.SendPropertyChanging();
			entity.User = this;
		}
        public decimal CalculateTotalKilometers(Car car)
        {
            List<Refuelling> refuellings = RefuellingRepository.List(car);
            refuellings.Reverse();

            decimal returnValue = 0;
            int index = 0;
            while (returnValue == 0 && refuellings.Count > 0)
            {
                returnValue = refuellings[index].Kilometers;
                index++;
            }

            return returnValue;
        }
 public List<Refuelling> List(Car car)
 {
     return DataSource.DataContext.Refuellings.Where(r => r.Car == car).OrderBy(r => r.Date).ToList();
 }
 public void Update(Car car)
 {
     car.EnsureValid();
 }
        public ActionResult New(FormCollection form)
        {
            Car car = new Car();

            try
            {
                this.UpdateModel(car, new[] { "Make", "Model", "PurchasePrice", "LicensePlate", "FuelType", "Description" });
                CarRepository.Add(car);

                return RedirectToAction("Details", new { licensePlate = car.LicensePlate });
            }
            catch (RuleViolationException)
            {
                this.UpdateModelStateWithViolations(car, ViewData.ModelState);

                return View("New", car);
            }
        }
 public List<Refuelling> List(Car car)
 {
     return DataStore.Refuellings.Where(r => r.Car == car).OrderBy(r => r.Date).ToList();
 }