acctman
acctman

Reputation: 4349

How to determine if a list contains any objects

A simple IF statement I am not able to get to process for some reason. The following error is received:

Operator == cannot be applied to operands of type List<Status> and bool.

I just need to know if the statement above for statusCollection is true then process the rest call. Still fairly new to C# and .NET so I'm learning.

Expected Result

If any results are found for statusCollection = statusCollection.Where then run statement if nothing is found continue to console output.

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 + "\n\nLast Run: " + DateTime.Now.ToString("MM/dd/yyyy h:mm tt\n"));

        statusCollection = statusCollection.Where(r => r.Service_key == "cloud-networking" && r.Begin > DateTime.Now.AddHours(-24)).ToList();

        if (statusCollection == true) {

            var client = new RestClient("http://1.0.0.111:80/restapi/call");
            var request = new RestRequest(Method.POST);
            request.AddHeader("Postman-Token", "");
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Content-Type", "text/xml");
            request.AddParameter("undefined", "@\"\n<event>\n    " +
                "<title>GCP Networking Status</title>\n    " +
                "<description>Google Cloud Platform Newtwork Status</description>\n    " +
                "<application></application>\n    " +
                "<state>Open</state>\n    " +
                "<sub_category></sub_category>\n    " +
                "<hint></hint>\n    " +
                "</related_hints>\n</event>\"@", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
        }

        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("MM/dd/yyyy h:mm tt"));
    }
}

Upvotes: 0

Views: 101

Answers (1)

Amir Molaei
Amir Molaei

Reputation: 3820

You are comparing a list with a bool. If you want to check whether the collection has any items or not then use:

if (statusCollection.Any())

Instead of:

if (statusCollection == true)

Upvotes: 5

Related Questions