public List<ServicePerson> GetByFilter(PersonSearchFilter personSearchFilter) { using (var context = new GeneralModelContainer()) { Guid parishId = personSearchFilter.ParishString.ToGuid(); if (parishId != Guid.Empty) { var sourceBll = new SourceDal(); sourceBll.GetParishSourceRecords(parishId) .ForEach(p => personSearchFilter.SourceString += "," + p.SourceId.ToString()); personSearchFilter.SourceString = personSearchFilter.SourceString.Substring(1); } var inMemoryLocFilter = ""; if (personSearchFilter.Location.Contains(',')) { inMemoryLocFilter = personSearchFilter.Location; personSearchFilter.Location = ""; } var temp = context.PersonsFiltered2(personSearchFilter.CName, personSearchFilter.Surname, personSearchFilter.FatherChristianName, personSearchFilter.FatherSurname, personSearchFilter.MotherChristianName, personSearchFilter.MotherSurname, personSearchFilter.SourceString, personSearchFilter.Location, personSearchFilter.LowerDate, personSearchFilter.UpperDate, personSearchFilter.County, personSearchFilter.SpouseChristianName, personSearchFilter.OthersideChristianName, personSearchFilter.OthersideSurname, personSearchFilter.OthersideRelationship, personSearchFilter.IsIncludeSources) .Select(p => new ServicePerson { PersonId = p.Person_id, ChristianName = p.ChristianName, Surname = p.Surname, BirthLocation = p.BirthLocation, Birth = p.BirthDateStr, Baptism = p.BaptismDateStr, Death = p.DeathDateStr, DeathLocation = p.DeathLocation, FatherChristianName = p.FatherChristianName, FatherSurname = p.FatherSurname, MotherChristianName = p.MotherChristianName, MotherSurname = p.MotherSurname, SourceDescription = p.Source, BirthYear = (p.BirthInt == 0) ? p.BapInt : p.BirthInt, DeathYear = p.DeathInt, LinkedTrees = p.tree_links, Occupation = p.Occupation, SpouseChristianName = p.SpouseName, SpouseSurname = p.SpouseSurname, Spouse = p.SpouseName + " " + p.SpouseSurname, FatherOccupation = p.FatherOccupation, Events = p.TotalEvents.ToString(CultureInfo.InvariantCulture), UniqueReference = p.UniqueRef.GetValueOrDefault().ToString(), SourceDateInt = p.SourceDateInt, SourceDateStr = p.SourceDateStr, SourceParishName = p.SourceParishName, SourceRef = p.SourceRef, SourceId = p.RefSourceId, OthersideChristianName = p.OthersideChristianName, OthersideSurname = p.OthersideSurname, OthersideRelationship = p.OthersideRelationship, ReferenceDate = p.ReferenceDateStr, ReferenceLocation = p.ReferenceLocation, ReferenceYear = p.ReferenceDateInt, MarriageId = p.marriageId.GetValueOrDefault().ToString() }).ToList(); if (inMemoryLocFilter.Length > 0) { //filter by multiple locations in memory cause the sql is too much a pain temp = temp.Where( servicePerson => inMemoryLocFilter.Split(',') .ToList() .Any( l => servicePerson.BirthLocation.LazyContains(l) || servicePerson.DeathLocation.LazyContains(l))).ToList(); } if (personSearchFilter.IsIncludeDeaths) temp = temp.Where(p => p.DeathYear > 0).ToList(); if (personSearchFilter.IsIncludeBirths) temp = temp.Where(p => p.BirthYear > 0).ToList(); return temp; } }