English 中文(简体)
Linqy no matchy
原标题:

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

最佳回答

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;
问题回答

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".





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

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. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签