Search This Blog

Tuesday, January 28, 2014

Single LINQ C# Example

C# > LINQ > Single

Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

Example

            string[] cars = { "audi", "ford", "mercedes", "dacia" };
            try
            {
                string car = cars.Single(c => c == "ford");
            }
            catch (System.InvalidOperationException)
            {
                MessageBox.Show ("Car not found!");
            }