private async Task <object> GetAttributeDistances(int attributeId, string address)
        {
            RockContext           rockContext           = new RockContext();
            AttributeValueService attributeValueService = new AttributeValueService(rockContext);
            LocationService       locationService       = new LocationService(rockContext);

            var attributeValues = attributeValueService.Queryable()
                                  .Where(av => av.AttributeId == attributeId && !string.IsNullOrEmpty(av.Value))
                                  .ToList();

            var locationGuids = attributeValues
                                .Select(a => a.Value.AsGuidOrNull())
                                .Where(g => g != null)
                                .ToList();

            var locations = locationService.Queryable()
                            .Where(l => locationGuids.Contains(l.Guid))
                            .ToList();

            var destinations = attributeValues
                               .Select(av => new Destination
            {
                EntityId   = av.EntityId,
                LocationId = locations.Where(l => l.Guid == av.Value.AsGuid()).Select(l => l.Id).FirstOrDefault()
            })
                               .Where(d => d.LocationId.HasValue && d.EntityId.HasValue)
                               .ToList();
            var output = await BingDistanceMatrix.OrderDestinations(address, destinations);

            return(output.ToDictionary(d => d.EntityId.ToString(), d => d.TravelDistance.ToString()));
        }
Beispiel #2
0
        public static async Task <List <CampusCache> > OrderCampusesByDistance(string origin)
        {
            var campusDestinations = CampusCache.All()
                                     .Select(c => new Destination
            {
                Address = string.Format("{0} {1} {2}, {3} {4}", c.Location.Street1, c.Location.Street2, c.Location.City, c.Location.State, c.Location.PostalCode),
                Entity  = c
            })
                                     .ToList();

            var distances = await BingDistanceMatrix.OrderDestinations(origin, campusDestinations);

            return(distances.Select(d => d.Entity as CampusCache).ToList());
        }
Beispiel #3
0
        public static async Task <List <CampusCache> > OrderCampusesByDistance(string origin)
        {
            var campusDestinations = CampusCache.All()
                                     .Where(c => c.Location != null && !string.IsNullOrEmpty(c.Location.Street1))
                                     .Select(c => new Destination
            {
                Address  = string.Format("{0} {1} {2}, {3} {4}", c.Location.Street1, c.Location.Street2, c.Location.City, c.Location.State, c.Location.PostalCode),
                EntityId = c.Id
            })
                                     .ToList();

            var distances = await BingDistanceMatrix.OrderDestinations(origin, campusDestinations);

            return(distances.Select(d => CampusCache.Get(d.EntityId ?? 0)).ToList());
        }
        public static async Task <List <Destination> > GetGroupsDestinations(string origin, IQueryable <Group> groups, RockContext rockContext, List <int> locationTypeIds = null)
        {
            var groupLocationQueryable = new GroupLocationService(rockContext).Queryable();

            if (locationTypeIds != null && locationTypeIds.Any())
            {
                groupLocationQueryable = groupLocationQueryable.Where(gl => locationTypeIds.Contains(gl.GroupLocationTypeValueId ?? 0));
            }

            var locationQueryable = new LocationService(rockContext).Queryable().Where(l => l.PostalCode != null && l.PostalCode != "");

            var destinations = groups
                               .Join(groupLocationQueryable,
                                     g => g.Id,
                                     gl => gl.GroupId,
                                     (g, gl) => new
            {
                Group         = g,
                GroupLocation = gl
            })
                               .Join(locationQueryable,
                                     a => a.GroupLocation.LocationId,
                                     l => l.Id,
                                     (a, l) => new
            {
                Group         = a.Group,
                GroupLocation = a.GroupLocation,
                Location      = l
            })
                               .DistinctBy(a => a.Group.Id)
                               .Select(a => new Destination
            {
                Location = a.Location,
                Entity   = a.Group
            })
                               .ToList();

            return(await BingDistanceMatrix.OrderDestinations(origin, destinations));
        }