public IQueryable <Plant> SearchPlants(Dictionary <string, string> searchCriteria) { var session = NHibernateHelper.GetCurrentSession(); IQueryable <Plant> speciesQuery = null; IQueryable <Plant> plantQuery = null; foreach (KeyValuePair <string, string> kvp in searchCriteria) { switch (kvp.Key) { #region free text case "FREETEXT_SEARCH_KEY": { List <long> speciesIdsForMatchingTaxas = GetSpeciesWhereParentMatchs(kvp.Value); if (speciesQuery == null) { if (string.IsNullOrEmpty(kvp.Value)) { speciesQuery = session.Query <Plant>(); } else { speciesQuery = session.Query <Plant>().Where(s => s.Name.ToLower().Contains(kvp.Value.ToLower()) || s.Description.ToLower() .Contains(kvp.Value.ToLower()) || s.ScientificName.ToLower() .Contains(kvp.Value.ToLower()) || speciesIdsForMatchingTaxas.Contains(s.Id)); } } else { if (string.IsNullOrEmpty(kvp.Value)) { speciesQuery = session.Query <Plant>(); } else { speciesQuery = speciesQuery.AsQueryable().Where(s => s.Name.ToLower().Contains(kvp.Value.ToLower()) || s.Description.ToLower().Contains(kvp.Value.ToLower()) || s.ScientificName.ToLower() .Contains(kvp.Value.ToLower()) || speciesIdsForMatchingTaxas.Contains(s.Id)); } } break; } #endregion free text #region plants #region Time case "Sowing": { var sowings = getMatchingTimePeriods(GetAll <Sowing>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => sowings.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => sowings.Contains(s))); } break; } case "Harvest": { var harvest = getMatchingTimePeriods(GetAll <Harvest>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => harvest.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => harvest.Contains(s))); } break; } case "Bloom": { var bloom = getMatchingTimePeriods(GetAll <Bloom>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => bloom.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => bloom.Contains(s))); } break; } case "SeedMaturity": { var seedMaturity = getMatchingTimePeriods(GetAll <SeedMaturity>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => seedMaturity.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => seedMaturity.Contains(s))); } break; } case "Cultivate": { var cultivates = getMatchingTimePeriods(GetAll <Cultivate>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => cultivates.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => cultivates.Contains(s))); } break; } case "Implant": { var implants = getMatchingTimePeriods(GetAll <Implant>(), TimePeriodHelper.GetMonth(Convert.ToInt32(kvp.Value))); if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => p.TimePeriods.Any(s => implants.Contains(s))); } else { plantQuery = plantQuery.AsQueryable().Where(p => p.TimePeriods.Any(s => implants.Contains(s))); } break; } #endregion Time #region Properties case "NutrientClaim": { if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => ((int)p.NutrientClaim).Equals(Convert.ToInt32(kvp.Value))); } else { plantQuery = plantQuery.AsQueryable().Where(p => ((int)p.NutrientClaim).Equals(Convert.ToInt32(kvp.Value))); } break; } case "RootDepth": { if (plantQuery == null) { plantQuery = session.Query <Plant>().Where(p => ((int)p.RootDepth).Equals(Convert.ToInt32(kvp.Value))); } else { plantQuery = plantQuery.AsQueryable().Where(p => ((int)p.RootDepth).Equals(Convert.ToInt32(kvp.Value))); } break; } #endregion Properties #endregion plants #region interactions //case "PositivInteractionOn": // { // if (speciesQuery == null) // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), POSITIV_PREDICATE_ROOT_ID); // else // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), POSITIV_PREDICATE_ROOT_ID, speciesQuery); // break; // } //case "NegativInteractionOn": // { // if (speciesQuery == null) // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), NEGAVTIV_PREDICATE_ROOT_ID); // else // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), NEGAVTIV_PREDICATE_ROOT_ID, speciesQuery); // break; // } //case "DoPositivInteraction": // { // if (speciesQuery == null) // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), POSITIV_PREDICATE_ROOT_ID); // else // speciesQuery = getSubjectsOfInteractionWithObject(Convert.ToInt64(kvp.Value), POSITIV_PREDICATE_ROOT_ID, speciesQuery); // break; // } //case "DoNegativInteraction": // { // if (speciesQuery == null) // speciesQuery = getObjectsOfInteractionWithSubject(Convert.ToInt64(kvp.Value), NEGAVTIV_PREDICATE_ROOT_ID); // else // speciesQuery = getObjectsOfInteractionWithSubject(Convert.ToInt64(kvp.Value), NEGAVTIV_PREDICATE_ROOT_ID, speciesQuery); // break; // } #endregion interactions } } if (speciesQuery != null && plantQuery != null) { return((speciesQuery.ToList().Intersect(plantQuery.ToList())).AsQueryable()); } if (speciesQuery != null) { return(speciesQuery); } if (plantQuery != null) { return(plantQuery); } return(session.Query <Plant>()); }