问题描述
假设我有一个列表框,它不断更新并包含例如:
Apple Juice -- 18 EURO
Orange Juice -- 14 EURO
Juice Berry -- 12 EURO
Juice Dates-- 56 EURO
我有一个文本框,我会在其中写下这个词:
Apple
Dates
我想要的输出是:
Apple Juice -- 18 EURO
Juice Dates -- 56 EURO
为了消除混淆,我正在尝试构建一个过滤器,在其中我有很多果汁类型,并通过输入一个合适的果汁类型,它会从大列表中搜索果汁类型并过滤它们并将它们显示为一个独立的优惠列表
到目前为止我尝试过的:
MyClass result = list.Find(x => x.Id == "Apple");
解决方法
winforms 中的文本框有一个 Lines 属性返回一个字符串数组。使用它,您可以使用
过滤string[] terms = myTextBox.Lines;
var result = list.Find(x => terms.Contains(x.Id));
或
var result = list.Find(x => Array.FindIndex(terms,t => t == x.Id) != -1);
Find
仅返回找到的第一个项目。您可以使用 FindAll
返回包含所有匹配项的列表。