Esempio n. 1
0
        public House GetMinConsumedHouse(DateTime startDate, DateTime endDate)
        {
            var result = m_context.WaterValues.Where(x => x.ValueDate >= startDate && x.ValueDate <= endDate)
                         .OrderBy(x => x.ValueDate);
            IEnumerable <IGrouping <WaterMeter, WaterValue> > query = result.GroupBy(x => x.WaterMeter, x => x);

            double minConsumption         = 0;
            bool   consumptionIntializied = false;
            var    actualHouse            = new House();

            actualHouse = null;

            foreach (IGrouping <WaterMeter, WaterValue> wmGroup in query)
            {
                WaterMeter wm = wmGroup.Key;
                // Iterate over each value in the
                // IGrouping and print the value.
                WaterValue wv1         = wmGroup.First();
                WaterValue wv2         = wmGroup.Last();
                double     consumption = wv2.Value - wv1.Value;

                if (wm.WMHouse != null)
                {
                    if (!consumptionIntializied)
                    {
                        minConsumption         = consumption;
                        actualHouse            = wm.WMHouse;
                        consumptionIntializied = true;
                    }
                    else if (consumption < minConsumption)
                    {
                        actualHouse    = wm.WMHouse;
                        minConsumption = consumption;
                    }
                }
            }

            return(actualHouse);
        }
Esempio n. 2
0
        public WaterMeter AddWaterMeterByHouseID(long houseID, WaterMeter newWaterMeter)
        {
            var house = m_context.Houses.Find(houseID);

            if (house == null)
            {
                return(null);
            }

            if (house.WaterMeter != null)
            {
                house.WaterMeter.WaterValues.Clear();
                m_context.WaterMeters.Remove(house.WaterMeter);
            }

            house.WaterMeter      = newWaterMeter;
            newWaterMeter.WMHouse = house;
            m_context.WaterMeters.Add(newWaterMeter);
            m_context.SaveChanges();

            return(newWaterMeter);
        }