0

How would I filter the output (Console.WriteLine(statusCollection...) to only show results if s.Service_name equals "cloud_networking" in the past 24hrs (s.Begin date is formatted like this 2018-05-19T04:39:59Z

class Program
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    //public static void Main()
    public static void Main(string[] args)
    {
        using (var webClient = new WebClient())
        {

            String rawJSON = webClient.DownloadString("https://status.cloud.google.com/incidents.json");
            List<Status> statusCollection = JsonConvert.DeserializeObject<List<Status>>(rawJSON);
            Console.WriteLine(statusCollection.Count + "Last Run:" + DateTime.Now.ToString("MM/dd/yyyy h:mm tt"));
            //Console.WriteLine(statusCollection.Count + " items");

            Console.WriteLine(string.Join("\n", statusCollection.Select(s => string.Format("{0} {1} ({2}) {3} - {4} - {5} updates",
                                                   s.Begin, s.Number, s.Severity, s.Service_name, s.External_desc, s.Updates.Count))));
            log.Info(DateTime.Now.ToString("\n MM/dd/yyyy h:mm tt"));
        }
    }
}

public class Status
{
    public string Begin { get; set; }
    public string Created { get; set; }
    public string End { get; set; }
    public string External_desc { get; set; }
    public string Modified { get; set; }
    [JsonProperty("most-recent-update")]
    public MRUpdateContainer Most_recent_update { get; set; }
    public int Number { get; set; }
    public bool Public { get; set; }
    public string Service_key { get; set; }
    public string Service_name { get; set; }
    public string Severity { get; set; }
    public List<Update> Updates { get; set; }
    public string Uri { get; set; }
}

public class MRUpdateContainer
{
    public string Created { get; set; }
    public string Modified { get; set; }
    public string Text { get; set; }
    public string When { get; set; }
}
public class Update
{
    public string Created { get; set; }
    public string Modified { get; set; }
    public string Text { get; set; }
    public string When { get; set; }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
acctman
  • 4,229
  • 30
  • 98
  • 142
  • Most commonly, LINQ is used to filter collections. What problem did you encounter when attempting to use LINQ's Where? I see that you are using Select so you both know and use LINQ – Camilo Terevinto Apr 23 '19 at 19:17
  • @CamiloTerevinto hi camilo, i attempted Console.WriteLine(statusCollection.Select(s.Service_name = "cloud_networking"... but no luck with that. I'm fairly new to .net so i'm learning as I got a long – acctman Apr 23 '19 at 19:21

1 Answers1

1

(1) You can change Begin property type to DateTime so it is easier to filter based on it. Json.Net can deserialize ISO 8601 dates to DateTime:

public DateTime Begin { get; set; }

(2) After deserializing collection, you can filter data with Linq .Where -operation as following:

DateTime start = DateTime.Now.AddHours(-24);
statusCollection = statusCollection.Where(r => r.Service_name == "cloud_networking" && r.Begin > start).ToList();
Risto M
  • 2,919
  • 1
  • 14
  • 27
  • 1
    thanks for taking the time to help me out. everything worked perfectly and I learned some new techniques from you in the process. – acctman Apr 23 '19 at 19:48