将LINQ结果显示为字符串 [英] Display LINQ result as string

查看:83
本文介绍了将LINQ结果显示为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试编写一个程序,该程序在文本文件中搜索多个关键字,然后在ListBox中列出匹配项.

我正在测试的文本文件的内容为:这是.不是测试

我有匹配权,但是我很难从LINQ中显示字符串.在下面的代码中,只有最后一个匹配项才会显示在列表框上,因为for循环每次都会为匹配项"分配一个新值.

我尝试的任何其他操作最终都会在每次匹配项中显示(Collection)"或以"[System.String]"结尾的内容.

如果不清楚,我深表歉意.我是新来的.我搜索了所有内容,只想将结果添加到列表中,但找不到任何有效的方法.

如果仅"select word.ToString()"有效,我可以做listBox1.Items.Add(matches);
但是,那也不起作用.非常感谢您提供一些帮助.


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.


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;
}

推荐答案

按如下所示更改代码-
Change your code as following--
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.


我认为您正在弄乱 LINQ和字符串示例 [ ^ ]或类似的文件.
无论如何,请更改以下内容

I assume you are messing with LINQ and strings sample[^] or a similar one in there.
Anyway, change following

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

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>


非常感谢您的帮助. :)
Thank you two very much for your help. :)


这篇关于将LINQ结果显示为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆