Avatar of eli411
eli411

asked on 

Null Exception c#

I had following block of code that I got error out and don't know how to handle the null exception.

            internal static ApplicationCustomField GetFieldByLabel(this IEnumerable<ApplicationCustomField> fields, string label, bool caseSensitive = false)
            {
            //Created a string to compare
                  var stringcomparison = caseSensitive ? StringComparison.CurrentCulture : StringComparison.InvariantCultureIgnoreCase;

                  var query = fields.Where(acf => acf.Label.Equals(label, stringcomparison));

            switch (query.Count())
            {
                case 0: return null;
                case 1: return query.First();
                default: throw new InvalidOperationException();
            }

            }


---- it error out on the case 0: return null;
.NET ProgrammingC#LINQ QuerySQL

Avatar of undefined
Last Comment
eli411

8/22/2022 - Mon