DEV Community

KrishnaSai Polanki
KrishnaSai Polanki

Posted on

2 1

Single, SingleOrDefault, First and FirstOrDefault in LINQ

Single

  • Returns the result only when predicate matches the collection that contains only one matching record.
    public List<string> Names = new List<string>() { "Jon", "Amber"};

    public string GetName()
    {
        return Names.Single(n=>n.Equals("Jon"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Throws error if the collection contains more than one matching data or no matching data.
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.Single(n=>n.Contains("Jon"));
    }
Enter fullscreen mode Exit fullscreen mode
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.Single(n=>n.Contains("ABC"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Loops through all the data of the collection even if it found the match in collection which has impact on performance.

SingleOrDefault

  • This is similar to the Single except returning default value when the specified condition not satisfied.
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.SingleOrDefault(n=>n.Contains("ABC"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Returns the null(default of string type) instead of throwing error.

First

  • Returns the result only when predicate matches the collection even that contains more than one matching record.
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.First(n=>n.Contains("Jon"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Throws error if the collection contains no matching data.
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.First(n=>n.Contains("ABC"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Will not loops through all the data of the collection once it found the match in collection thus making this is better than Single in terms of performance.

FirstOrDefault

  • This is similar to the First except returning default value when the specified condition not satisfied.
    public List<string> Names = new List<string>() { "Jon", "Amber", "Depp Jon","Heard Amber" };

    public string GetName()
    {
        return Names.FirstOrDefault(n=>n.Contains("ABC"));
    }
Enter fullscreen mode Exit fullscreen mode
  • Returns the null(default of string type) instead of throwing error.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay