Hi All
Maybe it's something I'm doing wrong. I'm just learning Linq because I'm bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control.
usage: enter text into textbox, click button. program lets you select a file to match the textbox value against and returns matches in label control.
code:
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();
}
}
}
The Problem What I have above works perfectly, except for one thing. I have a file that I check against which contains the following text:
File:
jason
is
the
funniest
person
in
the
world
jason
jason
jason
pezzimenti
... And it never, ever returns "jason". but it will always return any other word in there.
I'm guessing that it doesn't return a match if there is more that one of the same match?
Would I be correct in saying so? And is this how it's supposed to be? And how would you suggest I make it always return a match no matter how many of the same matches there are. I mean I would have thought that it would return the following output, based on the code above... isn't that what the foreach(Item in Query) is for?, when i type "jason" into the textBox1:
jason
jason
jason
jason
..but it doesn't return any jasons :(