views:

96

answers:

2
+2  Q: 

Linqy no matchy.

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 :(

+1  A: 

You probably have a blank at the end of the line... try that instead :

            IEnumerable<String> Query =
                from instance in Content
                where instance.Trim() == Value.Trim()
                orderby instance
                select instance;
Thomas Levesque
yeah i do have a blank at the end of the line BUT it correctly matches every other work. it just doesnt match a word if the file contains multiple instances of that word
baeltazor
From looking at your code, I'm just not buying the "only if it contains multiple instances" explanation. I'd test that by adding another pair of identical words to your file and report back here whether those words are displayed or not.
Mike Powell
i don't fully understand but thank you! :)
baeltazor
+1  A: 

You are correct in what you expect to happen, ie, you would get one line displayed per instance of the word in the text file.

It could be that you have blank spaces at the end of your file, as Thomas Levesque has stated, but it could also be that your file does not have the line endings that File.ReadAllLines() is expecting. It expects CRLF endings, so if you have only LF endings for example, you might fine that the method only returns one "line".

Ch00k
That makes sense. Thomas's solution worked for me, so maybe it was the spaces at the end of the line
baeltazor
but thats really interesting im going to go and read up on those crlf things (are they like... pizzas or something?) +1 for your detailed and helpful answer :)
baeltazor