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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:27:23+00:00 2026-06-08T02:27:23+00:00

Lets say I have a model, Article that has a large amount of columns

  • 0

Lets say I have a model, Article that has a large amount of columns and the database contains more than 100,000 rows. If I do something like var articles = db.Articles.ToList() it is retrieving the entire article model for each article in the database and holding it in memory right?

So if I am populating a table that only shows the date of the entry and it’s title is there a way to only retrieve just these columns from the database using the entity framework, and would it be more efficient?

According to this,

There is a cost required to track returned objects in the object
context. Detecting changes to objects and ensuring that multiple
requests for the same logical entity return the same object instance
requires that objects be attached to an ObjectContext instance. If you
do not plan to make updates or deletes to objects and do not require
identity management , consider using the NoTracking merge options when
you execute queries.

it looks like I should use NoTracking since the data isn’t being changed or deleted, only displayed. So my query now becomes var articles = db.Articles.AsNoTracking().ToList(). Are there other things I should do to make this more efficient?

Another question I have is that according to this answer, using .Contains(...) will cause a large performance drop when dealing with a large database. What is the recommended method to use to search through the entries in a large database?

  • 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-08T02:27:25+00:00Added an answer on June 8, 2026 at 2:27 am

    It’s called a projection and just translates into a SELECT column1, column2, ... in SQL:

    var result = db.Articles
        .Select(a => new
        {
            Date = a.Date,
            Title = a.Title
        })
        .ToList();
    

    Instead of a => new { ... } (creates a list of “anonymous” objects) you can also use a named helper class (or “view model”): a => new MyViewModel { ... } that contains only the selected properties (but you can’t use a => new Article { ... } as an entity itself).

    For such a projection you don’t need AsNoTracking() because projected data are not tracked anyway, only full entity objects are tracked.

    Instead of using Contains the more common way is to use Where like:

    var date = DateTime.Now.AddYears(-1);
    var result = db.Articles
        .Where(a => date <= a.Date)
        .Select(a => new
        {
            Date = a.Date,
            Title = a.Title
        })
        .ToList();
    

    This would select only the articles that are not older than a year. The Where is just translated into a SQL WHERE statement and the filter is performed in the database (which is as fast as the SQL query is, depending on table size and proper indexing, etc.). Only the result of this filter is loaded into memory.

    Edit

    Refering to your comment below:

    Don’t confuse IEnumerable<T>.Contains(T t) with string.Contains(string subString). The answer you have linked in your question talks about the first version of Contains. If you want to search for articles that have the string "keyword" in the text body you need the second Contains version:

    string keyword = "Entity Framework";
    var result = db.Articles
        .Where(a => a.Body.Contains(keyword))
        .Select(a => new
        {
            Date = a.Date,
            Title = a.Title
        })
        .ToList();
    

    This will translate into something like WHERE Body like N'%Entity Framework%' in SQL. The answer about the poor performance of Contains doesn’t apply to this version of Contains at all.

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

Sidebar

Related Questions

Lets say we have a Company model, that has many Employees And has many
Let's say I have a set of Django Models: class Article(models.Model): title = models.CharField(max_length=100,
Lets say I have a function in my model, that generates a style tag
Let's say we have an Article model and a Comment model. Article: columns: body:
Lets say I have a ConsumerModel . ConsumerModel has many Model properties, and one
Lets say I have a site that has a Layout with navigation. This navigation
Lets say we have a simple model that stores two integers, the min and
Lets say I have a project model which has many members and many tasks.
Let's say I have 3 models (Note, Comment, Article) that I want to search
Lets say I have model inheritance set up in the way defined below. class

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.