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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:40:53+00:00 2026-05-18T10:40:53+00:00

Given the following LINQ Statement(s), which will be more efficient? ONE: public List<Log> GetLatestLogEntries()

  • 0

Given the following LINQ Statement(s), which will be more efficient?

ONE:

public List<Log> GetLatestLogEntries()
{
    var logEntries = from entry in db.Logs
                 select entry;
    return logEntries.ToList().Take(10);
}

TWO:

public List<Log> GetLatestLogEntries()
{
    var logEntries = from entry in db.Logs
                 select entry;
    return logEntries.Take(10).ToList();
}

I am aware that .ToList() executes the query immediately.

  • 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-18T10:40:54+00:00Added an answer on May 18, 2026 at 10:40 am

    The first version wouldn’t even compile – because the return value of Take is an IEnumerable<T>, not a List<T>. So you’d need it to be:

    public List<Log> GetLatestLogEntries()
    {
        var logEntries = from entry in db.Logs
                     select entry;
        return logEntries.ToList().Take(10).ToList();
    }
    

    That would fetch all the data from the database and convert it to a list, then take the first 10 entries, then convert it to a list again.

    Getting the Take(10) to occur in the database (i.e. the second form) certainly looks a heck of a lot cheaper to me…

    Note that there’s no Queryable.ToList() method – you’ll end up calling Enumerable.ToList() which will fetch all the entries. In other words, the call to ToList doesn’t participate in SQL translation, whereas Take does.

    Also note that using a query expression here doesn’t make much sense either. I’d write it as:

    public List<Log> GetLatestLogEntries()
    {
        return db.Log.Take(10).ToList();
    }
    

    Mind you, you may want an OrderBy call – otherwise it’ll just take the first 10 entries it finds, which may not be the latest ones…

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

Sidebar

Related Questions

The following LINQ statement: public override List<Item> SearchListWithSearchPhrase(string searchPhrase) { List<string> searchTerms = StringHelpers.GetSearchTerms(searchPhrase);
Given the following LINQ to SQL query: var test = from i in Imports
Given the following code: var people = new List<person>(){ new person { Name =
I get the following error : Message: No value given for one or more
I have an odd linq subquery issue. Given the following data structure: Parents Children
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the
Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following information, how can I combine these 2 linq queries into 1.
I have the following group by linq statement from c in Categories join p
I am trying to do the following: Given TypeInfo after reflecting on a LINQ-to-SQL

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.