Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

arrays - 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"); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...