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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:28:09+00:00 2026-05-31T21:28:09+00:00

My question is really simple. When should I use List, IEnumerable and ArrayList. Here’s

  • 0

My question is really simple. When should I use List, IEnumerable and ArrayList.

Here’s my scenario. I’m working in a Web app using LINQ. Information is returned as a IEnumerable:

IEnumerable<Inventory> result = from Inventory i in db where.... 

I’m not sure how IEnumerable works, but every operation takes a lot of time to execute. More specifically, result.Count(), result.ElementAt(i), result.ToList, etc, each operation takes a considerable amount of time.

So, I was wondering if I should treat this as a List by doing result.ToList, instead of working with the IEnumerable variable.

Thanks!

  • 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-31T21:28:10+00:00Added an answer on May 31, 2026 at 9:28 pm

    If I understand what you’re doing correctly, you have a query like from Inventory i in db select i and then you do several operations on the result:

    var count = result.Count();
    var fifth = result.ElementAt(5);
    var allItems = result.ToList();
    

    Now consider what happens when you have the query as different types:

    • IQueryable<T>

      var result = from Inventory i in db select i;
      IQueryable<Inventory> result = from Inventory i in db select i;
      

      The two lines above are the same. They don’t actually go to the database, they just create a representation of the query. If you have this, Count() will execute an SQL query like SELECT COUNT(*) FROM Inventory, ElementAt(5) will execute another query that takes only the fifth item in the table and ToList() will execute something like SELECT * FROM Inventory, but that’s what we want here.

    • IEnumerable<T>

      IEnumerable<Inventory> result = from Inventory i in db select i;
      

      Doing this again does not go the database, it only creates a representation of the query. But it’s a representation that can’t use the methods specific to IQueryable<T>, so any LINQ operation will enumerate the collection, which will execute an SQL query like SELECT * FROM Inventory.

      So, for the example: Count() will execute the SELECT * … query only to count the items in the result. ElementAt(5) will execute the whole query again, only to throw away all items except for the fifth. And ToList() will execute the query one more time.

    • List<T>

      List<Inventory> result = (from Inventory i in db select i).ToList();
      

      This will actually execute the SELECT * FROM Inventory query immediately and once. All operations you do with result won’t touch the database, they will be done in-memory.

    What should you take away from this? First, never use IEnumerable<T> as the type of the database query. It has horrible performance.

    If you want to make several distinct operations on the result, using IQueryable<T> might be the best solution.

    If you want to retrieve the whole result anyway, use ToList() (or ToArray()) as soon as possible and then work with the resulting List<T>.

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

Sidebar

Related Questions

Simple question really - how do I use text_field_with_auto_complete inside a form_for block? I've
This is a simple question really. I've been using the new type of constructors
Question is really simple. What is Request.InputStream and when to use it. Is it
Really simple question: Am I missing something? Seems like this should be all that
The question is really simple: should a path to a directory always contain a
Don't let below code scare you away . The question is really simple, only
Really simple question - how do I do a search to find all records
simple question really, i was wanting to know what naming conventions anybody puts on
Really simple question: From within GWT I want to forward the user away from
This is probably a really simple question but... I would like to do a

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.