views:

176

answers:

3

I have a fair amount of questions and my first one is how can I do a simple LINQ query to match a word in a file? I'm not trying to be stupid but i haven't understood the documentation that I found for LINQ properly.

+3  A: 

What about something like the following?

string yourFileContents = File.ReadAllText("c:/file.txt");
string foundWordOrNull =  Regex.Split(yourFileContents, @"\w").FirstOrDefault(s => s == "someword");

(who ever said that C# cannot be concise?)

Te code works by reading your file, splitting it into words and then returning the first word it finds that's called someword.

EDIT: From a comment the above was considered "not LINQ". Though I don't concur (see comments), I do think a more LINQified example of the same approach is warranted here ;-)

string yourFileContents = File.ReadAllText("c:/file.txt");
var foundWords =  from word in Regex.Split(yourFileContents, @"\w")
                  where word == "someword"
                  select word;

if(foundWords.Count() > 0)
    // do something with the words found
Abel
I don't mind you downvoting, but be so courteous to explain why or what's wrong with the code.
Abel
I down-voted you because the user specifically asked for a Linq sample. Not Regular Expressions.
baeltazor
Thanks for explaining. I consider `FirstOrDefault` part of Linq. The asker did not specify that it was forbidden to use helpers. Other examples use `String.Split`, which comes down to the same. It is not possible to split with Linq (ok, it is, but it'll become tedious on the char array).
Abel
i'm sorry about the down-vote. i still feel bad about it. and thanks for also explaining that FirstOrDefault is linq i did not know that :) thank you
baeltazor
Don't feel bad, it's good and makes me look twice at my example and rethink its educational value ;-). That's what it is for. Btw, you can change your downvote after I edit the q. anyway, if you feel it's better now.
Abel
+1 (A very strange thing just occured. I clicked UPvote, and it went from 1 to 3) I've never seen that before. Anyhow - That's a great example Abel. I have one question, if you don't mind, is there a performance difference worthy of notice between String.Split() and Regex.Split()? :)
baeltazor
Strange indeed, I believed it was already on two, perhaps a cache issue? The performance issue will not be noticeable in this example (you may even find that the regex performs quicker in certain scenarios). If performance is really important: profiling and measuring are your friend (and you don't want to use LINQ anyway with perf. critical apps that do text parsing).
Abel
yeah i guesss its a chache issue. thats interesting about the regex, im going to go look into it some more, i love reading about performance-related things in c#
baeltazor
+1  A: 

create a new WindowsForms application and use the following code.

you''ll need to add a label label control, textbox, and a button

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace LinqTests
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public 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.Trim() == Value.Trim()
                    orderby instance
                    select instance;

                foreach (String Item in Query)
                    label1.Text +=
                        Item + Environment.NewLine;
            }
            else Application.DoEvents();

            ofile.Dispose();
        }
    }
}

i hope this helps

baeltazor
um yeah. That does actually help me out. Thanks
Nice example. Note that it looks for a line that equals `Value` (`textBox1.Text`), not for a word, as you asked in your q. Change the line with `instance.Trim()` to `where instance.Trim().Contains(Value)` or something similar.
Abel
Thank you for the correction Abel. Much appreciated.
baeltazor
+1  A: 

Here is an example from MSDN that counts occurrences of a word in a string (http://msdn.microsoft.com/en-us/library/bb546166.aspx).

string text = ...;

string searchTerm = "data";

//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
    StringSplitOptions.RemoveEmptyEntries);

// Create and execute the query. It executes immediately 
// because a singleton value is produced.
// Use ToLowerInvariant to match "data" and "Data" 
var matchQuery = from word in source
         where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
         select word;

// Count the matches.
int wordCount = matchQuery.Count();
Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.",
    wordCount, searchTerm);

And here is one more LINQ tutorial on reading data from text file http://www.onedotnetway.com/tutorial-reading-a-text-file-using-linq/.

dreikanter