Click here to Skip to main content
15,896,359 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I am trying to write a program that searches a text file for multiple keywords, then lists the matches in a ListBox.

The text file I am testing this on has contents of: this is. not a test

I have the matching right but I am having trouble getting a string to show up from LINQ. With the code I have below, only the last match will show up on the ListBox because the for loop assigns a new value to 'matches' every time.

Anything else I try ends up showing "(Collection)" or something that ends in "[System.String]" for each match.

I apologize if I am unclear; I am new to this. I have searched all over and just want to add the results to a list but I can't find anything that works.

If only "select word.ToString()" worked I could do listBox1.Items.Add(matches);
But, that doesn't work either. I would very much appreciate some help on this.


C#
List<string> matches = new List<string>();

string sourceText = System.IO.File.ReadAllText(fileName);

string[] words = sourceText.Split(new char[] { '.', '?', '!', ',', ' ',
    ';', '\'', ':', '"', '-' }, StringSplitOptions.RemoveEmptyEntries);

string[] wordsToMatch = { "this", "is", "a", "test" };

for (int i = 0; i < wordsToMatch.Count(); i++)
{
    matches = (from word in words
             where word.ToLowerInvariant() == wordsToMatch[i].ToLowerInvariant()
             select word).ToList();
 }

foreach (string s in matches)
{
    listBox1.DataSource = matches;
}
Posted

Change your code as following--
C#
List <string> matches = new List<string>();
 
string sourceText = System.IO.File.ReadAllText(fileName);
 
string[] words = sourceText.Split(new char[] { '.', '?', '!', ',', ' ',
    ';', '\'', ':', '"', '-' }, StringSplitOptions.RemoveEmptyEntries);
 
List<string> wordsToMatch = new List<string>(new string[] { "this", "is", "a", "test" });
 
 matches = (from word in words
             where wordsToMatch.Contains(word.ToLowerInvariant())
             select word).ToList();
 
    foreach (string s in matches)
    {
      listBox1.Items.Add(s);
    }

//or now you can use
//listBox1.DataSource=matches;
// but loop is not required in this case.
 
Share this answer
 
v3
I assume you are messing with LINQ and strings sample[^] or a similar one in there.
Anyway, change following

C#
string[] wordsToMatch = { "this", "is", "a", "test" };
for (int i = 0; i < wordsToMatch.Count(); i++)
{
    matches = (from word in words
             where word.ToLowerInvariant() == wordsToMatch[i].ToLowerInvariant()
             select word).ToList();
 }
foreach (string s in matches)
{
    listBox1.DataSource = matches;
}


to
C#
List<string> wordsToMatch = new List<string>()
wordsToMatch.AddRange(new string[]{ "this", "is", "a", "test" });
string[] matches = (from word in words
         where wordsToMatch.Contains(word)
         select word).ToArray();
listBox1.DataSource = matches;
</string></string>
 
Share this answer
 
Thank you two very much for your help. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900