Advertisement
Guest User

Yield return

a guest
Oct 16th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class TimesheetRow
  9.     {
  10.         public int Hour { get; set; }
  11.         public int Minute { get; set; }
  12.         public int Second { get; set; }
  13.         public int Distance { get; set; }
  14.         public string Origin { get; set; }
  15.     }
  16.  
  17.     class Program
  18.     {
  19.         private static IEnumerable<TimesheetRow> ParseFile(string path)
  20.         {
  21.             using (var reader = new StreamReader(path))
  22.             {
  23.                 string line;
  24.                 while ((line = reader.ReadLine()) != null)
  25.                 {
  26.                     var parts = line.Split(' ');
  27.                     yield return
  28.                         new TimesheetRow
  29.                         {
  30.                             Hour = int.Parse(parts[0]),
  31.                             Minute = int.Parse(parts[1]),
  32.                             Second = int.Parse(parts[2]),
  33.                             Distance = int.Parse(parts[3]),
  34.                             Origin = parts[4]
  35.                         };
  36.                 }
  37.             }    
  38.         }
  39.  
  40.         private static void Main(string[] args)
  41.         {
  42.             var list = ParseFile(args[0]).ToList();
  43.  
  44.             Console.WriteLine("Parsed {0} rows.", list.Count);
  45.  
  46.             var top5 = list.OrderByDescending(r => r.Distance).Take(5).ToList();
  47.  
  48.             Console.WriteLine("Top {0} rows by distance:", top5.Count);
  49.             top5.ForEach(
  50.                 row => Console.WriteLine("{0:D2}:{1:D2}:{2:D2} {3} - {4}",
  51.                     row.Hour, row.Minute, row.Second,
  52.                     row.Origin, row.Distance));
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement