也许这是我做错了。我只是在学习Linq,因为我很无聊。迄今为止这么好。我做了一个小程序,它基本上只是将所有匹配(foreach)输出到一个标签控件中。Linqy no matchy
用法:在文本框中输入文本,单击按钮。程序让您选择一个文件来匹配文本框的值,并返回标签控件中的匹配项。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace LinqTests
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected internal String[]
Content;
public String
Value;
private void button1_Click(object sender, EventArgs e)
{
Value = textBox1.Text;
OpenFileDialog ofile = new OpenFileDialog();
ofile.Title = "Open File";
ofile.Filter = "All Files (*.*)|*.*";
if (ofile.ShowDialog() == DialogResult.OK)
{
Content =
File.ReadAllLines(ofile.FileName);
IEnumerable<String> Query =
from instance in Content
where instance == Value
orderby instance
select instance;
foreach (String Item in Query)
label1.Text +=
Item + Environment.NewLine;
}
else Application.DoEvents();
ofile.Dispose();
}
}
}
的问题 上面我所完美的作品,除了一两件事。我有我对证包含以下文本的文件:
文件:
杰森
是
的
最有趣的
人
在
的
世界
杰森
jason
jason
pezzimenti
...而它永远不会返回“杰森”。但它会一直返回任何其他单词。
我猜测如果有更多的相同的匹配,它不会返回匹配?
我说得对吗?这是它应该如何?无论有多少场比赛,你会如何建议我总能让比赛回来?我的意思是我会认为它会返回以下输出,根据上面的代码...是不是什么foreach(查询中的项目)?,当我在文本框1中键入“jason”时:
杰森
杰森
杰森
杰森
..但它不返回任何JASONS :(
为什么地狱是我投票? – 2009-11-13 03:20:09
你已经进入了一个严肃的论坛。 [stoic face]这里不允许轻浮。 :P对于轻浮,请参阅meta.stackoverflow.com。 – 2009-11-13 03:24:58
查看LINQPad - 这比创建应用程序来测试查询要快得多。 http://www.linqpad.net/ – TrueWill 2009-11-13 03:25:06