我的网页上有一个“超级搜索”选项。这只是跨多个属性搜索匹配。随着时间的推移,它已经从简单的精确文本匹配发展到以任何顺序检查单个单词,并将下划线视为空格和多个空格(如单个空格),它现在变得非常混乱,并不适合所有情况!
当前的Linq过滤代码:
private List<StockRecordItem> GetCoreSearchResults()
{
//strip multiple spaces
string searchString = Regex.Replace(uxTxtSuperSearch.Text.Trim(), " +", " ");
List<StockRecordItem> stocks = StoresPortalDataAccessHelper.GetAllStockRecords(uxLocation.SelectedValue)
//Check no super search provided
.Where(s =>
(string.IsNullOrEmpty(searchString)
|| (
// Check Stock description - treating underscores and ampersands as spaces
searchString.ToLower().Split().All(w => s.Description.ToLower().Replace(",", " ").Replace("_", " ").Replace("&", " ").Replace("-", " ").Split().Contains(w))
// Check Stock Notes - treating underscores and ampersands as spaces
|| (s.Notes != null && searchString.ToLower().Split().All(w => s.Notes.ToLower().Replace(",", " ").Replace("_", " ").Replace("&", " ").Replace("-", " ").Split().Contains(w)))
// Check other single word fields
|| s.StockId.Contains(searchString, StringComparison.OrdinalIgnoreCase)
|| s.Description.Contains(searchString, StringComparison.OrdinalIgnoreCase)
|| s.Notes.Contains(searchString, StringComparison.OrdinalIgnoreCase)
|| s.Warehouse.Contains(searchString, StringComparison.OrdinalIgnoreCase)
|| s.IsRestricted.ToString().Contains(searchString.ToLower().Replace("yes", "True").Replace("no", "False"), StringComparison.OrdinalIgnoreCase)
|| (!string.IsNullOrEmpty(s.Manufacturer) && s.Manufacturer.Contains(searchString, StringComparison.OrdinalIgnoreCase))
|| s.RelatedEquipments.Any(e => e.EquipmentNumber.Contains(searchString, StringComparison.OrdinalIgnoreCase))
|| s.RelatedEquipments.Any(e => e.EquipmentId.Contains(searchString, StringComparison.OrdinalIgnoreCase)))
)
// Part Number filter
&& (string.IsNullOrEmpty(uxTxtPartNumber.Text) || s.PartNumber.Contains(uxTxtPartNumber.Text, StringComparison.OrdinalIgnoreCase))
// Warehouse filter
&& (string.IsNullOrEmpty(uxWarehouse.SelectedItem.Text) || s.Warehouse.Contains(uxWarehouse.SelectedItem.Text, StringComparison.OrdinalIgnoreCase))
)
.ToList();
return stocks;
}
所需功能:
问题: