Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Understanding Single, SingleOrDefault, First and FirstOrDefault

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 302
    Comment on it

    There are element operators provided by LINQ which are capable of returning single or specific element from a collection. These element operators can be defined as follows :-

     

    • Single : This element operator is capable of returning single specific element from a collection of elements when searched element match is found. Whenever no match or more than one match is found for searched element in the collection, an exception is thrown.

     

    • SingleOrDefault : It is capable of returning single specific element from a collection of elements when searched element match is found or returns a default value when no match is found. In case more than one match for searched element is found, an exception is thrown. The default value depends on type of data.

     

    • First : In this case whenever more than one match is found for searched element, first single specific element is returned. Whenever no match for searched element in the collection, an exception is thrown.

     

    • FirstOrDefault : In this case whenever more than one match is found for searched element, first single specific element is returned, in case no match is found for searched element in the collection, a default value is returned. The default value depends on type of data.

     

    Example to demonstrate Single, SingleOrDefault, First and FirstOrDefault :-

     

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                List<int> data = new List<int> { 1, 2, 3, 4, 5 };
                try
                {
                    Console.WriteLine("The output of First element operator {0}",data.First(d => d <= 2));
                    Console.WriteLine("The output of FirstOrDefault element operator {0}",data.FirstOrDefault(d => d >= 3));
                    Console.WriteLine("The output of Single element operator {0}",data.Single(d => d == 5));
                    Console.WriteLine("The output of SingleOrDefault element operator {0}", data.SingleOrDefault(d => d == 10));
                }
                catch(Exception e)
                {
                    Console.WriteLine("An exception occurred {0}",e);
                }
                Console.ReadKey();
            }
    }

     

    Output:

    The output of First element operator 1
    The output of FirstOrDefault element operator 3
    The output of Single element operator 5
    The output of SingleOrDefault element operator 0

     

    An exception is thrown if list input is changed :

     

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main()
            {
                List<int> data = new List<int> { 1, 1, 3, 4, 5 };
                try
                {
                    Console.WriteLine("The output of First element operator {0}",data.First(d => d <= 2));
                    Console.WriteLine("The output of FirstOrDefault element operator {0}",data.FirstOrDefault(d => d >= 3));
                    Console.WriteLine("The output of Single element operator {0}",data.Single(d => d == 5));
                    Console.WriteLine("The output of SingleOrDefault element operator {0}", data.SingleOrDefault(d => d == 1));
                }
                catch(Exception e)
                {
                    Console.WriteLine("An exception occurred {0}",e);
                }
                Console.ReadKey();
            }
    }

     

    Output :

    The output of First element operator 1
    The output of FirstOrDefault element operator 3
    The output of Single element operator 5
    An exception occurred System.InvalidOperationException: Sequence contains more t
    han one matching element
       at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func
    `2 predicate)
       at ConsoleApplication1.Program.Main() in d:\Deepika\TrainingaspC#\ConsoleAppl
    ication1\ConsoleApplication1\Program.cs:line 22
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: