Rock.Model.GroupService.GetNearestGroup C# (CSharp) Method

GetNearestGroup() public method

Gets the nearest group.
public GetNearestGroup ( int personId, int groupTypeId ) : Group
personId int The person identifier.
groupTypeId int The group type identifier.
return Group
        public Group GetNearestGroup( int personId, int groupTypeId )
        {
            var rockContext = (RockContext)this.Context;
            var personService = new PersonService( rockContext );
            var personGeopoint = personService.GetGeopoints( personId ).FirstOrDefault();
            if ( personGeopoint != null )
            {
                var groupLocation = this.Queryable()
                    .Where( g =>
                        g.GroupTypeId.Equals( groupTypeId ) )
                    .SelectMany( g =>
                        g.GroupLocations
                            .Where( gl =>
                                gl.Location != null &&
                                gl.Location.GeoPoint != null
                            )
                    )
                    .OrderBy( gl => gl.Location.GeoPoint.Distance( personGeopoint ) )
                    .FirstOrDefault();

                if ( groupLocation != null )
                {
                    return groupLocation.Group;
                }
            }

            return null;
        }