Why is this code throwing an InvalidOperationException?
I think that my code should make the ViewBag.test property equal to "No Match" , but instead it throws an InvalidOperationException . Why is this? string str = "Hello1,Hello,Hello2"; string another = "Hello5"; string retVal = str.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) .First(p => p.Equals(another)); if (str == another) { ViewBag.test = "Match"; } else { ViewBag.test = "No Match"; //this does not happen when it should } As you can see here , the First method throws an InvalidOperationException when the sequence on which it is called is empty. Since no element of the