c# - 使用带有 OR 语句的 Find All 过滤列表
问题描述
if (product_search.Text.Trim() != "")
{
filteredProductsList = filteredProductsList
.FindAll(s => s.product.Contains(product_search.Text.Trim().ToUpper()));
}
如果产品包含搜索的文本,我有上面的代码过滤产品列表。但是,我希望此文本框在Product
或上进行过滤Barcode
。如果有办法运行FindAll()
但有一个OR
声明。那么过滤到产品是否包含搜索的文本或条形码?
解决方案
像这样的东西:
// Search if product_search is not all whitespace string
if (!string.IsNullOrWhiteSpace(product_search.Text)) {
// let's extract a local variable (readability)
string toFind = product_search.Text.Trim();
// it seems you want case insensetive search: let's specify it clearly:
// StringComparison.CurrentCultureIgnoreCase
// trick: IndexOf(..., StringComparison.CurrentCultureIgnoreCase) >= 0 since
// we don't have appropriate Contains
// Finally, you want || - either product or barCode should contain toFind
filteredProductsList = filteredProductsList
.FindAll(item =>
item.product.IndexOf(toFind, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
item.barCode.IndexOf(toFind, StringComparison.CurrentCultureIgnoreCase) >= 0);
}
推荐阅读
- kubernetes - 使用 Kubernetes 部署移动应用后端
- python - Matplotlib 在新线程中绘图
- python - Django:分区表的外键
- javascript - 使用 JavaScript Onclick 事件将数据传递给 Django 中的views.py?
- php - twig 在使用 if 语句时不会加载我的 HTML 输入
- django - 与芹菜搭配时,Keras 在训练期间悬挂
- angular - 在 Angular 6 中使用 HTTP 客户端访问响应标头时遇到问题
- amazon-web-services - TestNG 注释在 AWS 设备场中被忽略
- load-testing - Telegram Bot 负载测试
- mysql - 使用 Cron 将数据从 mysql 数据库导出到 CSV,然后将所有数据获取到 bigquery 表