public static List <T> SearchByProperties <T>(this List <T> list, SearchModel entity) { IEnumerable <T> result = list; foreach (PropertyInfo p in entity.GetType().GetProperties()) { var value = p.GetValue(entity); if (value != null && !value.Equals("") && !value.Equals(0)) { result = result.Where(e => { var val = e.GetType().GetProperty(p.Name).GetValue(e); if (val != null) { if (p.PropertyType.Equals(typeof(string))) { return(val.ToString().Contains(value.ToString())); } return(val.Equals(value)); } else { return(false); } }); } } return(result.ToList()); }
private bool HasSearchCriteria(SearchModel searchModel) { //Using a bit of reflection to check if any of the properties are not empty var type = searchModel.GetType(); var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); var hasProperty = properties.Select(x => x.GetValue(searchModel, null)) .Any(y => y != null && !String.IsNullOrWhiteSpace(y.ToString())); return(hasProperty); }
public static void BeforeFeature() { HttpController.Init(() => "http://www.bing.com"); HttpSteps.AddVariableLookupShortKey("SearchModel", typeof(SearchModel).FullName); var searchModel = new SearchModel { Term = "tree" }; FeatureContext.Current.Add(searchModel.GetType().FullName, searchModel); }
/// <summary> /// 获得 <see cref="SearchModel"/> 中过滤条件 /// </summary> /// <returns></returns> protected IEnumerable <IFilterAction> GetAdvanceSearchs() { var searchs = new List <IFilterAction>(); if (ShowAdvancedSearch && CustomerSearchModel == null && SearchModel != null) { var searchColumns = Columns.Where(i => i.Searchable); foreach (var property in SearchModel.GetType().GetProperties().Where(i => searchColumns.Any(col => col.GetFieldName() == i.Name))) { var v = property.GetValue(SearchModel); if (v != null && v.ToString() != string.Empty) { searchs.Add(new SearchFilterAction(property.Name, v, FilterAction.Equal)); } } } return(searchs); }
public async Task <IEnumerable <Car> > GetListedCars(SearchModel searchModel, bool active) { var properties = searchModel.GetType().GetProperties(); var carsList = await this.context.Cars.ToListAsync(); for (int i = 0; i < properties.Length; i++) { if (properties[i].GetValue(searchModel) != null) { switch (properties[i].Name.ToLower().ToString()) { case "bodytype": carsList = carsList.Where(x => x.BodyType == (BodyType)int.Parse(searchModel.BodyType)).ToList(); break; case "brandtype": carsList = carsList = carsList.Where(x => x.BrandId == int.Parse(searchModel.BrandType)).ToList(); break; case "modeltype": carsList = carsList.Where(x => x.ModelId == int.Parse(searchModel.ModelType)).ToList(); break; case "fueltype": carsList = carsList.Where(x => x.FuelType == (FuelType)int.Parse(searchModel.FuelType)).ToList(); break; case "transmissiontype": carsList = carsList.Where(x => x.Transmission == (TransmissionType)int.Parse(searchModel.TransmissionType)).ToList(); break; case "minyear": carsList = carsList.Where(x => x.Year.Year >= int.Parse(searchModel.MinYear)).ToList(); break; case "minprice": carsList = carsList.Where(x => x.Price >= int.Parse(searchModel.MinPrice)).ToList(); break; case "maxyear": carsList = carsList.Where(x => x.Year.Year <= int.Parse(searchModel.MaxYear)).ToList(); break; case "maxprice": carsList = carsList.Where(x => x.Price <= int.Parse(searchModel.MaxPrice)).ToList(); break; default: break; } } } carsList = carsList.Where(x => x.IsActive == active).ToList(); return(carsList); }
public string GetUrl(int i) { var searchUrl = "?"; if (SearchModel != null) { Type type = SearchModel.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { if (property.PropertyType == typeof(List <int>)) { var cats = (List <int>)property.GetValue(SearchModel); if (cats != null) { foreach (var item in cats) { searchUrl += property.Name + "=" + item + "&"; } } } else { if (property.GetValue(SearchModel) != null) { searchUrl += property.Name + "=" + property.GetValue(SearchModel) + "&"; } } } } var result = ""; if (!string.IsNullOrEmpty(SortBy)) { if (searchUrl.Contains("SortBy=") || searchUrl.Contains("sortby=")) { var sb = searchUrl.Contains("SortBy=") ? "SortBy=" : "sortby"; searchUrl = searchUrl.Substring(0, searchUrl.IndexOf(sb)); } result += "sortby=" + SortBy + "&"; if (SortDir == "desc") { result += "sortdir=desc&"; } if (searchUrl.Contains("SortDir=") || searchUrl.Contains("sortdir=")) { var sb = searchUrl.Contains("SortDir=") ? "SortDir" : "sortdir"; searchUrl = searchUrl.Substring(0, searchUrl.IndexOf(sb)); } } if (i > 0) { result += "page=" + i + "&"; } PageSizes size; var succ = Enum.TryParse(PageSize.ToString(), out size); if (succ && size != PageSizes.P20 && size != 0) { result += "pagesize=" + PageSize + "&"; } result = result.Substring(result.Length - 1) == "&" ? result.Substring(0, result.Length - 1) : result; if (!string.IsNullOrEmpty(result)) { if (result.Substring(0, 1) == "&" && searchUrl.Substring(searchUrl.Length - 1) == "?") { result = result.Substring(1, result.Length - 1); } result = searchUrl + result; } if (!string.IsNullOrEmpty(ActionLink)) { result = ActionLink + result; } return(result); }