Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 565137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:50:25+00:00 2026-05-13T12:50:25+00:00

I am a bit curious as to which is considered best practice when it

  • 0

I am a bit curious as to which is considered “best practice” when it comes to FirstOrDefault.

I’ve already seen this question, which is similar to the question I have, but not close enough that it answers my question.

Which of these is “better code”? and why?

var foos = GetMyEnumerableFoos();

var foo1 = (from f in foos
     where f.Bar == "spider monkey"
     select f).FirstOrDefault();

/* OR */

var foo2 = foos.FirstOrDefault(f => f.Bar == "spider monkey");

I tend toward the latter, as IMO, it makes for cleaner code. But I’m curious as to whether or not the techincal “guts” of what’s going on there is more efficient the other way. Does this change if you’re using different types of IEnumerables? like DataTables or string arrays or LINQ objects?

=========edit==========

Assuming Jon Skeet’s post is correct, I went looking into Reflector to see what Where and FirstOrDefault look like, and here’s what I came up with:

In the case of foos.Where(f => f.Bar == “spider monkey”).FirstOrDefault()

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    if (predicate == null)
    {
        throw Error.ArgumentNull("predicate");
    }
    if (source is Iterator<TSource>)
    {
        return ((Iterator<TSource>) source).Where(predicate);
    }
    if (source is TSource[])
    {
        return new WhereArrayIterator<TSource>((TSource[]) source, predicate);
    }
    if (source is List<TSource>)
    {
        return new WhereListIterator<TSource>((List<TSource>) source, predicate);
    }
    return new WhereEnumerableIterator<TSource>(source, predicate);
}

which would feed into:

public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    IList<TSource> list = source as IList<TSource>;
    if (list != null)
    {
        if (list.Count > 0)
        {
            return list[0];
        }
    }
    else
    {
        using (IEnumerator<TSource> enumerator = source.GetEnumerator())
        {
            if (enumerator.MoveNext())
            {
                return enumerator.Current;
            }
        }
    }
    return default(TSource);
}

In the case of foos.FirstOrDefault(f => f.Bar == “spider monkey”);

public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    if (predicate == null)
    {
        throw Error.ArgumentNull("predicate");
    }
    foreach (TSource local in source)
    {
        if (predicate(local))
        {
            return local;
        }
    }
    return default(TSource);
}

Looking at that still leaves me a bit confused, would there be efficiencies added by using the proper iterator for certain object types? Or is it more efficient to skip all that and just start looping through and testing? My gut again tells me it’s the latter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:50:25+00:00Added an answer on May 13, 2026 at 12:50 pm

    Well, the “select” part will be removed by the compiler anyway, so you’re really comparing:

    foo.Where(f => f.Bar == "spider monkey")
       .FirstOrDefault()
    

    vs

    foo.FirstOrDefault(f => f.Bar == "spider monkey")
    

    I doubt that it will make any significant difference to efficiency within LINQ to Objects anyway. I’d personally use the latter version myself… unless I wanted to reuse the filtering part of the query elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 350k
  • Answers 350k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The built-in pickle module can do it if you pass… May 14, 2026 at 7:08 am
  • Editorial Team
    Editorial Team added an answer The problem could be: your internet connection (some firewall blocking… May 14, 2026 at 7:08 am
  • Editorial Team
    Editorial Team added an answer viewDidUnload is only called when your application receives a memory… May 14, 2026 at 7:08 am

Related Questions

Currently I am working with a custom business object layer (adopting the facade pattern)
I'm investigating the differences between using log4net and System.Diagnostics.Trace for logging, and I'm curious
I have a project which I am building with Maven which uses Hibernate (and
I am planning a PHP application that needs to store date/times in an MSSQL
I have a few questions related to Windows processes in kernel and usermode. If

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.