Rock.Model.GroupMemberService.GetByPersonId C# (CSharp) Method

GetByPersonId() public method

Returns a queryable collection of Rock.Model.GroupMember entities associated with a Rock.Model.Person by the Person's PersonId
public GetByPersonId ( int personId ) : IQueryable
personId int An representing the Id of the to search by.
return IQueryable
        public IQueryable<GroupMember> GetByPersonId( int personId )
        {
            return Queryable( "Person", true ).Where(  t => t.PersonId == personId );
        }

Usage Example

        /// <summary>
        /// Handles the RowCommand event of the grdPersonSearchResults control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="GridViewCommandEventArgs"/> instance containing the event data.</param>
        protected void rGridPersonResults_AddExistingPerson( object sender, GridViewCommandEventArgs e )
        {
            if ( e.CommandName == "Add" )
            {
                var rockContext = new RockContext();
                GroupMemberService groupMemberService = new GroupMemberService( rockContext );
                int index = int.Parse( e.CommandArgument.ToString() );
                int personId = int.Parse( rGridPersonResults.DataKeys[index].Value.ToString() );

                var family = CurrentCheckInState.CheckIn.Families.Where( f => f.Selected ).FirstOrDefault();
                if ( family != null )
                {
                    var checkInPerson = new CheckInPerson();
                    checkInPerson.Person = new PersonService( rockContext ).Get( personId ).Clone( false );
                    var isPersonInFamily = family.People.Any( p => p.Person.Id == checkInPerson.Person.Id );
                    if ( !isPersonInFamily )
                    {
                        if ( personVisitorType.Value != "Visitor" )
                        {
                            // TODO: DT: Is this right?  Not sure this is the best way to set family id
                            var groupMember = groupMemberService.GetByPersonId( personId ).FirstOrDefault();
                            groupMember.GroupId = family.Group.Id;

                            rockContext.SaveChanges();

                            checkInPerson.FamilyMember = true;
                            hfSelectedPerson.Value += personId + ",";
                        }
                        else
                        {
                            AddVisitorGroupMemberRoles( family, personId );
                            checkInPerson.FamilyMember = false;
                            hfSelectedVisitor.Value += personId + ",";
                        }
                        checkInPerson.Selected = true;
                        family.People.Add( checkInPerson );
                        ProcessFamily();
                    }

                    mpeAddPerson.Hide();
                }
                else
                {
                    string errorMsg = "<ul><li>You have to pick a family to add this person to.</li></ul>";
                    maWarning.Show( errorMsg, Rock.Web.UI.Controls.ModalAlertType.Warning );
                }
            }
            else
            {
                mpeAddPerson.Show();
                BindPersonGrid();
            }
        }