none
Difference between Single, SingleOrDefault, First and FirstOrDefault RRS feed

  • Question

  • http://www.dotnettricks.com/learn/linq/understanding-single-singleordefault-first-and-firstordefault

    Single

    It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.

    SingleOrDefault

    It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.

    in case of single they said if no data found or more match found then exception will be thrown....that i understand but in case of singleordefault

    tell me when exception will be thrown? A default value is returned, if no match is found....what would be the default value ? null

    null will be return when no match found ?

    First

    It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.

    FirstOrDefault

    It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.

    thanks

    Thursday, December 14, 2017 6:05 PM

Answers

  • Hi Sudip_inn,

    >>tell me when exception will be thrown? 

    If more than one match found for that element in the collection, Single and SingleOrDefault all throw exception,  if no match is found in the collection, Single will throw exception, but SingleOrDefault does not throw exception, it will return a default value(zero or null).

    >>A default value is returned, if no match is found....what would be the default value ? null

    zero or null, For example.

    #Models

     public partial class User
        {
            public int UserId { get; set; }
    
            [Required]
            public string UserName { get; set; }
    
            public int Age { get; set; }
        }

    #Usage

    using (var db = new SalesContext())
    {
    //if database does not have a record with UserId 1
    
    //return null
      var query = db.Users.Where(t => t.UserId == 1).SingleOrDefault();
    
    //return 0
      var query2 = db.Users.Where(t => t.UserId == 1).Select(t=>t.Age).SingleOrDefault();
    
    }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, December 15, 2017 1:34 PM
    Friday, December 15, 2017 1:50 AM
    Moderator

All replies

  • Hi Sudip_inn,

    >>tell me when exception will be thrown? 

    If more than one match found for that element in the collection, Single and SingleOrDefault all throw exception,  if no match is found in the collection, Single will throw exception, but SingleOrDefault does not throw exception, it will return a default value(zero or null).

    >>A default value is returned, if no match is found....what would be the default value ? null

    zero or null, For example.

    #Models

     public partial class User
        {
            public int UserId { get; set; }
    
            [Required]
            public string UserName { get; set; }
    
            public int Age { get; set; }
        }

    #Usage

    using (var db = new SalesContext())
    {
    //if database does not have a record with UserId 1
    
    //return null
      var query = db.Users.Where(t => t.UserId == 1).SingleOrDefault();
    
    //return 0
      var query2 = db.Users.Where(t => t.UserId == 1).Select(t=>t.Age).SingleOrDefault();
    
    }

    Best regards,

    Zhanglong Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Friday, December 15, 2017 1:34 PM
    Friday, December 15, 2017 1:50 AM
    Moderator
  • What if I want to return all elements that match the criteria?

    Monday, December 23, 2019 2:49 PM
  • What if I want to return all elements that match the criteria?

    Then you don't use any of the extensions/functions mentioned in the thread. 
    Tuesday, December 24, 2019 9:09 AM