コード例 #1
0
        public IQueryable<FamilySearchResult> GetFamiliesByPersonNameSearch( string searchString, int maxResults = 20 )
        {
            bool reversed;

            RockContext rockContext = new RockContext();
            PersonService personService = new PersonService( rockContext );
            Guid homeAddressGuid = Rock.SystemGuid.DefinedValue.GROUP_LOCATION_TYPE_HOME.AsGuid();

            // get list of people matching the search string
            IOrderedQueryable<Person> sortedPersonQry = personService
                .GetByFullNameOrdered( searchString, true, false, false, out reversed );

            var personResults = sortedPersonQry.AsNoTracking().ToList();

            List<FamilySearchResult> familyResults = new List<FamilySearchResult>();
            foreach ( var person in personResults )
            {
                var families = personService.GetFamilies( person.Id )
                                    .Select( f => new FamilySearchResult
                                                        {
                                                            Id = f.Id,
                                                            Name = f.Name,
                                                            FamilyMembers = f.Members.ToList(),
                                                            HomeLocation = f.GroupLocations
                                                                            .Where( l => l.GroupLocationTypeValue.Guid == homeAddressGuid )
                                                                            .OrderByDescending( l => l.IsMailingLocation )
                                                                            .Select( l => l.Location )
                                                                            .FirstOrDefault(),
                                                            MainPhone = f.Members
                                                                            .OrderBy( m => m.GroupRole.Order )
                                                                            .ThenBy( m => m.Person.Gender )
                                                                            .FirstOrDefault()
                                                                            .Person.PhoneNumbers.OrderBy( p => p.NumberTypeValue.Order ).FirstOrDefault()
                                                        } )
                                                        .ToList();

                foreach ( var family in families )
                {
                    familyResults.Add( family );
                }
            }

            return familyResults.DistinctBy( f => f.Id ).AsQueryable();
        }