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

  • SEARCH
  • Home
  • 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 7782391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:31:45+00:00 2026-06-01T19:31:45+00:00

So, here’s my code: Notice the positions of the ToList() method here, which is

  • 0

So, here’s my code:

Notice the positions of the ToList() method here, which is of IEnumerable, comparing it line by line.

Customers.ToList().Where(m=>m.ID > 3).OrderByDescending(m=>m.Name).FirstOrDefault();

Customers.Where(m=>m.ID > 3).ToList().OrderByDescending(m=>m.Name).FirstOrDefault();

Customers.Where(m=>m.ID > 3).OrderByDescending(m=>m.Name).ToList().FirstOrDefault();

Let’s go through it line by line:

Customers.ToList().Where(m=>m.ID > 3)
.OrderByDescending(m=>m.Name).FirstOrDefault()

  • .ToList() – Enumerable
  • .Where() – Enumerable
  • .OrderByDescending() – Enumerable
  • .FirstOrDefault – Enumerable

Customers.Where(m=>m.ID > 3).ToList()
.OrderByDescending(m=>m.Name).FirstOrDefault()

  • .Where() – Queryable
  • .ToList() – Enumerable
  • .OrderByDescending() – Enumerable
  • .FirstOrDefault() – Enumerable

Customers.Where(m=>m.ID > 3).OrderByDescending(m=>m.Name)
.ToList().FirstOrDefault()

  • .Where() – Queryable
  • .OrderByDescending() – Queryable
  • .ToList() – Enumerable
  • .FirstOrDefault() – Enumerable

Now, here are their SQL, by order:

SELECT [t0].[ID], [t0].[Name] FROM [Customer] AS [t0] GO

SELECT [t0].[ID], [t0].[Name] FROM [Customer] AS [t0] WHERE [t0].[ID]

SELECT TOP (1) [t0].[ID], [t0].[Name] FROM [Customer] AS [t0] WHERE
[t0].[ID] > @p0 ORDER BY [t0].[Name] DESC

It seems that line1 gets the ENTIRE collection and passes it through the wire, while line3 gets only ONE entity.

According to the SQL output, I can infer that:

    line1: Memory-intensive code; requires more bandwidth since more data is passed in the wire;
    line3: Database-intensive code; requires less bandwidth since less data is passed in the wire

I’m curious as to what goes on internally between each lines of codes. They are relatively similar, and I get the same exact results.

What exactly goes on in here (within the insides of C# and its features?)?

  • 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-06-01T19:31:47+00:00Added an answer on June 1, 2026 at 7:31 pm

    Methods like .Where() and .OrderBy() use deferred execution, which means that all they do when called is modify the expression tree for the query – they do not cause the query to be executed. The underlying query is only executed when it is enumerated by something (e.g. by doing a foreach on it).

    .ToList() on the other hand is intended to return an in-memory list of the query results, that is, it actually causes the query to be executed. It is conceptually similar to doing something like the following pseudocode

    foreach (item in query)
       list.add(item) 
    

    In your examples, the query is executed by the .ToList() and from that point on you are doing a new query, this time against the in-memory collection.

    So, in the 1st example the Customers query it is executed immediately by the ToList(), the .Where() and .OrderBy() just modify a new expression tree using the Linq to objects query provider and this Linq to objects query is executed by the FirstOrDefault().

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

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
Here a simple question : What do you think of code which use try
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
here is my php code $titikPetaInti = array(); while($row = mysql_fetch_assoc($hasil2)) { $titikPetaInti[] =
Here's a piece of code I copied from http://www.schillmania.com/content/projects/javascript-animation-1/demo/ Very simple JS animation: function

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.