Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace HVTest
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Program
- {
- static void Main(string[] args)
- {
- var manipulator = new WeatherManipulator();
- manipulator.Run();
- }
- }
- class WeatherManipulator
- {
- private IEnumerable<Weather> collection;
- public void Run()
- {
- collection = this.GenerateData();
- collection = collection.DateIs(DateTime.Now).CityIs("Tartu").ToList();
- foreach(var item in collection)
- Console.WriteLine(item.ToString());
- Console.ReadKey();
- }
- private List<Weather> GenerateData()
- {
- Random random = new Random();
- DateTime start = new DateTime(2012, 1, 1);
- DateTime end = new DateTime(2012, 4, 1).AddDays(-1);
- IEnumerable<City> cities = Enum.GetValues(typeof(City)).Cast<City>();
- List<Weather> result = new List<Weather>();
- foreach(var city in cities)
- {
- for (DateTime date = start; date <= end; date = date.AddDays(1)) result.Add(new Weather() { City = city, Date = date, Temperature = random.Next(-16, 3) });
- }
- return result;
- }
- }
- static class WeatherExtensions
- {
- public static IEnumerable<Weather> DateIs(this IEnumerable<Weather> collection, DateTime date)
- {
- foreach(var item in collection)
- {
- if (item.Date.Date == date.Date) yield return item;
- }
- }
- public static IEnumerable<Weather> DateIsLINQ(this IEnumerable<Weather> collection, DateTime date)
- {
- return collection.Where(item => item.Date.Date == date.Date);
- }
- public static IEnumerable<Weather> CityIs(this IEnumerable<Weather> collection, string city)
- {
- foreach(var item in collection)
- {
- if (item.City == (City)Enum.Parse(typeof(City), city, true)) yield return item;
- }
- }
- public static IEnumerable<Weather> CityIsLINQ(this IEnumerable<Weather> collection, string city)
- {
- return collection.Where(item => item.City == (City)Enum.Parse(typeof(City), city, true));
- }
- }
- class Weather
- {
- public DateTime Date { get; set; }
- public City City { get; set; }
- public int Temperature { get; set; }
- public override string ToString()
- {
- return String.Format("City: {0}, Date: {1}, Temperature: {2}", City, Date, Temperature);
- }
- }
- public enum City { Jõhvi, Narva, Tartu, Tallinn }
- }
Advertisement
Add Comment
Please, Sign In to add comment