简体   繁体   中英

Catch items from list which contain specifc string c#

lets assume I got a listbox which keeps updating and contains for example:

Apple Juice -- 18 EURO
Orange Juice -- 14 EURO
Juice Berry -- 12 EURO
Juice Dates-- 56 EURO

And I have a textbox, where I would write the term:


Apple
Dates

And my desired output would be:

Apple Juice -- 18 EURO
Juice Dates -- 56 EURO

To clear the confusion, I am trying to build a filter where I have lots of juice types and by typing a favorable juice type it would search for the juice types from the big list and filter them and display them into a standalone favorable list

What I have tryed so far:

MyClass result = list.Find(x => x.Id == "Apple"); 

The text box in winforms has a Lines property returning a string array. Using it, you could filter with

string[] terms = myTextBox.Lines;
var result = list.Find(x => terms.Contains(x.Id));

or

var result = list.Find(x => Array.FindIndex(terms, t => t == x.Id) != -1);

Find returns only the first item found. You can use FindAll to return a list with all matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM