Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If all you want is the keyword and a count of how many times they appear in the file, then lazyberezovsky's solution is about as elegant of a solution as you will find. But if you need to do any other metrics on the file's data, then I would load the file into a collection that keeps your other metadata intact.</p> <p>Something simple like:</p> <pre><code>var forecasts = File.ReadAllLines(file).Skip(1) // skip the header row .Select(line =&gt; line.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries)) // split the line into an array of strings .Select (f =&gt; new { Outlook = f[0], Temperature = f[1], Humidity = f[2], Windy = f[3], PlayTennis = f[4] }); </code></pre> <p>will give you an <code>IEnumerable&lt;&gt;</code> of an anonymous type that has properties that can be queried.</p> <p>For example if you wanted to see how many times "sunny" occurred in the Outlook then you could just use LINQ to do this:</p> <pre><code>var count = forecasts.Count( f =&gt; f.Outlook == "sunny"); </code></pre> <p>Or if you just wanted the list of all outlooks you could write:</p> <pre><code>var outlooks = forecasts.Select(f =&gt; f.Outlook).Distinct(); </code></pre> <p>Where this is useful is when you want to do more complicated queries like "How many rainy cool days are there?</p> <pre><code>var count = forecasts.Count (f =&gt; f.Outlook == "rain" &amp;&amp; f.Temperature == "cool"); </code></pre> <p>Again if you just want all words and their occurrence count, then this is overkill.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload