How to query templates from SendGrid with SDK

Here is a small sample of how to query all templates from SendGrid with the SendGrid SDK.

The porpose of the snippet is to find the dynamic template with name by the value of variable sendGridTemplateName. Then it returns the id of the template, to use it for sending the mail.

var response = await _sendGridClient.RequestAsync(SendGridClient.Method.GET, urlPath: "/templates?generations=dynamic");

            var body = await response.DeserializeResponseBodyAsync(response.Body);

            if (body.ContainsKey("templates"))
            {
                var templatesList = body["templates"];

                if (templatesList is IEnumerable<dynamic>) //handles null as well
                {
                    List<dynamic> templates = Enumerable.ToList<dynamic(templatesList);

                    foreach (var item in templates)
                    {
                        if (item.name == sendGridTemplateName)
                            return item.id.ToString();
                    }
                }
            }