超级搜索多个领域&通配符

时间:2013-08-30 01:48:54

标签: c# linq wildcard

我的网页上有一个“超级搜索”选项。这只是跨多个属性搜索匹配。随着时间的推移,它已经从简单的精确文本匹配发展到以任何顺序检查单个单词,并将下划线视为空格和多个空格(如单个空格),它现在变得非常混乱,并不适合所有情况!

当前的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;
}

所需功能:

  • 搜索完全匹配
  • 不区分大小写
  • 多个空格视为单个空格
  • 将下划线和&符号视为单个空格
  • 'MySearchString'将匹配'MySearchString记录项'或'MySearchString'
  • '我的搜索字符串'将匹配'String SEARCH My'或这些字的任何其他组合。 (注意:这与“字符串搜索”不匹配,每个单词必须存在)
  • 我可能没有提到的其他任何在搜索解决方案中都是好的做法?

问题:

  1. 如何清除上述代码以提高可读性/性能?
  2. 如何添加通配符选项。即搜索字符串'My%Item'返回'my item'和'myItem'

0 个答案:

没有答案