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 4615262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:48:53+00:00 2026-05-22T01:48:53+00:00

What is the difference between the two pieces of code below? This returns the

  • 0

What is the difference between the two pieces of code below?

This returns the data I expect….

return productTable.FirstOrDefault(p => p.ProductId == productId);

This doesn’t….

return productTable.Where(x => x.ProductId == productId).FirstOrDefault();

I’m mainly just wondering if there’s a logical difference between these two.

  • 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-22T01:48:54+00:00Added an answer on May 22, 2026 at 1:48 am

    Those queries should be essentially identical. The parameterless version of FirstOrDefault() just grabs the first record available from the query, or default value (i.e., null) if no records are available.

    Edit 2 As duly pointed out in the comments, I should be using LINQ-to-SQL. Here is a sample with LINQ-to-SQL:

    using (ProductsDataContext context = new ProductsDataContext())
    {
        context.Log = Console.Out;
        var p1 = context.Products.FirstOrDefault(p => p.ProductId == 1);
        var p2 = context.Products.Where(p => p.ProductId == 1).FirstOrDefault();
    }
    

    Output (notice the queries are exactly identical):

    SELECT TOP (1) [t0].[ProductId],
    [t0].[Name] FROM [dbo].[Products] AS
    [t0] WHERE [t0].[ProductId] = @p0
    — @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
    — Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build:
    4.0.30319.1

    SELECT TOP (1) [t0].[ProductId],
    [t0].[Name] FROM [dbo].[Products] AS
    [t0] WHERE [t0].[ProductId] = @p0
    — @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
    — Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build:
    4.0.30319.1

    Edit: Here is sample code to demonstrate they are the same thing:

    class Product
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            List<Product> productTable = new List<Product> {
                new Product { ProductId = 123, Name = "Cheese" },
                new Product { ProductId = 456, Name = "Milk" },
            };
    
            var r1 = productTable.FirstOrDefault(p => p.ProductId == 123);
            var r2 = productTable.Where(p => p.ProductId == 123).FirstOrDefault();
    
            // these print out the same thing
            Console.WriteLine(r1.Name);
            Console.WriteLine(r2.Name);
    
            Console.ReadLine();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between the two pieces of code below? Will there be
What is the difference between these two pieces of code type IInterface1 = interface
What is the difference between these two pieces of code? TYPE t_my_cursor IS REF
Is there a performance difference between the following two pieces of code? if (myCondition)
What's the difference between the following two pieces of HTML (apologies if there are
Is there a difference between the two examples below for beginning a Perl script?
How to calculate minute difference between two date-times in PHP?
I am feeling surprised by the difference between two seemingly identical scripts. first.ps1: A
I'm currently working on a class that calculates the difference between two objects. I'm
What is the easiest way to highlight the difference between two strings in PHP?

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.