public void UpdateMinCommercialSegments(double newMinCommSegPercent)
        {
            var comSegData  = CommercialSegmentQueryable.GetCommercialCarSemgents(DataContext, Parameters);
            var locations   = LocationQueryable.GetLocations(DataContext, Parameters);
            var carSegments = CarSegmentQueryable.GetCarSegments(DataContext, Parameters);

            var fullComSeg = from ccs in comSegData
                             from l in locations
                             from cs in carSegments
                             select new
            {
                LocationId   = l.dim_Location_id,
                CarSegmentId = cs.car_segment_id,
                ccs.CommercialCarSegmentId
            };

            var minCommIdsToUpdate = from fullCs in fullComSeg
                                     join mcs in DataContext.MinCommercialSegments on
                                     new { fullCs.LocationId, fullCs.CarSegmentId, fullCs.CommercialCarSegmentId }
            equals new { mcs.LocationId, mcs.CarSegmentId, mcs.CommercialCarSegmentId }
            select mcs;

            minCommIdsToUpdate.ForEach(d => d.Percentage = newMinCommSegPercent);
            minCommIdsToUpdate.ForEach(d => d.UpdatedBy  = ApplicationAuthentication.GetGlobalId());
            minCommIdsToUpdate.ForEach(d => d.UpdatedOn  = DateTime.Now);

            var entriesToCreate = from fullCs in fullComSeg
                                  join mcs in DataContext.MinCommercialSegments on
                                  new { fullCs.LocationId, fullCs.CarSegmentId, fullCs.CommercialCarSegmentId }
            equals new { mcs.LocationId, mcs.CarSegmentId, mcs.CommercialCarSegmentId }
            into joinedComSeg
            from comSegFigures in joinedComSeg.DefaultIfEmpty()
            where comSegFigures == null
                select new
            {
                LocationId             = fullCs.LocationId,
                CarSegmentId           = fullCs.CarSegmentId,
                CommercialCarSegmentId = fullCs.CommercialCarSegmentId,
                Percentage             = newMinCommSegPercent,
                LastChangedOn          = DateTime.Now,
                LastChangedBy          = ApplicationAuthentication.GetGlobalId()
            };
            var localEntities = from etc in entriesToCreate.AsEnumerable()
                                select new MinCommercialSegment
            {
                LocationId             = etc.LocationId,
                CarSegmentId           = etc.CarSegmentId,
                CommercialCarSegmentId = etc.CommercialCarSegmentId,
                Percentage             = newMinCommSegPercent,
                UpdatedOn = DateTime.Now,
                UpdatedBy = ApplicationAuthentication.GetGlobalId()
            };

            DataContext.MinCommercialSegments.InsertAllOnSubmit(localEntities);
            DataContext.SubmitChanges();
        }
Example #2
0
        public List <MinCommercialSegmentRow> GetMinCommercialSegments(int scenarioId)
        {
            var comSegData = CommercialSegmentQueryable.GetCommercialCarSemgents(DataContext, Parameters);

            var locations   = LocationQueryable.GetLocations(DataContext, Parameters);
            var carSegments = CarSegmentQueryable.GetCarSegments(DataContext, Parameters);


            var fullComSeg = from ccs in comSegData
                             from l in locations
                             from cs in carSegments
                             select new
            {
                LocationId   = l.dim_Location_id,
                CarSegmentId = cs.car_segment_id,
                ccs.CommercialCarSegmentId
            };

            var minComSegments =
                DataContext.MinCommercialSegments.Where(d => d.MinCommercialSegmentScenarioId == scenarioId);

            var rows = from fullCs in fullComSeg
                       join mcs in minComSegments on new { fullCs.LocationId, fullCs.CarSegmentId, fullCs.CommercialCarSegmentId }
            equals new { mcs.LocationId, mcs.CarSegmentId, mcs.CommercialCarSegmentId }
            into joinedComSeg
            from comSegFigures in joinedComSeg.DefaultIfEmpty()
            join l in DataContext.LOCATIONs on fullCs.LocationId equals l.dim_Location_id
            join cs in DataContext.CAR_SEGMENTs on fullCs.CarSegmentId equals cs.car_segment_id
            join ccs in DataContext.CommercialCarSegments on fullCs.CommercialCarSegmentId equals ccs.CommercialCarSegmentId

                select new MinCommercialSegmentRow
            {
                CarSegment        = cs.car_segment1,
                CommercialSegment = ccs.Name,
                Country           = cs.country,
                LastChangedBy     = comSegFigures == null ? string.Empty : comSegFigures.MarsUser.EmployeeId,
                LastChangedOn     = comSegFigures == null ? (DateTime?)null: comSegFigures.UpdatedOn,
                Location          = l.location1,
                LocationGroup     = l.CMS_LOCATION_GROUP.cms_location_group1,
                Percentage        = comSegFigures == null ? (double?)null : comSegFigures.Percentage,
                Pool = l.CMS_LOCATION_GROUP.CMS_POOL.cms_pool1
            };
            var returned = rows.ToList();

            return(returned);
        }