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);
}
我不确定这个filteredProductsList是什么类型的列表,但我会假设它是一个字符串列表,所以使用linq你可以做类似的事情
filteredProductsList = filteredProductsList.Where(x => x.product.Contains(toFind) || x.barcode.Contains(toFind)).ToList();
或者,如果你只是想要相反的选择,你可以做
filteredProductsList = filteredProductsList.Where(x => !(x.product.Contains(toFind) || x.barcode.Contains(toFind))).ToList();