Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use LINQ’s Select to shape each result into the values its caller needs. In EF Core, projecting specific properties can also let the database return fewer columns. For an API response, a named DTO makes that output shape explicit. Projection is not authorization, does not automatically create a tracked entity, and is subject to the query provider’s translation rules.

How do I select only the fields I need with LINQ?

Projection transforms each item into a different result form. Selecting a property returns that value; selecting an object initializer creates a new shape. For an in-memory IEnumerable<T>, these are C# transformations. With IQueryable<T>, such as an EF Core query, the provider receives an expression tree and must translate the supported parts.

var urls = blogs.Select(blog => blog.Url);

var cards = blogs.Select(blog => new
{
    blog.BlogId,
    blog.Name,
    blog.Url
});

Query syntax uses select for projection. SelectMany is different: it projects sequences and flattens them into one sequence. See Microsoft’s query expression basics.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Should I use an anonymous type or a DTO?

Choice Best fit Important limit
Anonymous type A convenient, local result shape used within the method or query. The compiler generates its type; source code cannot name it for a method return type or field. Its properties are public and read-only.
Named DTO An API response, a method boundary, or any result shape that benefits from an explicit, stable name. You define and maintain the contract, so include only fields the consumer needs.

Microsoft’s anonymous types guide covers their generated, local nature. ASP.NET Core’s Minimal API tutorial demonstrates DTOs for shaping responses. A named DTO is usually the clearer choice when the result leaves the query’s local scope.

How can I return a DTO from an EF Core query?

Project to the DTO inside the query so EF Core can translate the requested shape. Keep filters, ordering, and limits in the query before materializing results:

var blogCards = await db.Blogs
    .Where(b => b.Rating >= minimumRating)
    .OrderBy(b => b.Name)
    .Select(b => new BlogCardDto(b.BlogId, b.Name, b.Url))
    .ToListAsync();

public sealed record BlogCardDto(int Id, string Name, string Url);

This pattern asks for the fields used to construct each card rather than returning a full Blog entity. The exact SQL depends on the provider and expression; inspect the generated SQL when selected columns or translation matter.

Why is EF Core fetching every column?

If a query materializes an entity, EF Core generally needs the entity’s mapped properties. Microsoft’s efficient querying guidance shows a full entity selection replaced with Select(b => b.Url), which allows the illustrated query to select just the URL column. For several fields, project an anonymous type or DTO.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This is useful when the result does not need the rest of the entity. EF Core’s single and split queries guidance also explains that large columns can be duplicated in joined results; projection can omit columns the result does not use. Neither example establishes a universal speedup: the outcome depends on the query, provider, and data.

Does projection protect fields in an API response?

A DTO can keep a property out of a particular response by not including it. That is a useful data-shaping practice, not a complete security control. Your application must decide which caller may receive each value; projection implements the selected response shape but does not enforce authorization policy.

For example, an ordinary client response might omit a model’s Secret property, while an authorized administrative response could have a different DTO. The ASP.NET Core DTO tutorial also describes DTOs for reducing payloads, flattening nested graphs, and helping prevent over-posting.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Is a projected result tracked for updates?

Do not treat a DTO or anonymous projection as a tracked entity. EF Core’s change tracking works with entity instances; a projection that returns a different shape is not automatically the original tracked entity. The anonymous projection described in the single and split queries guidance is not tracked as the blog entity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an edit workflow, query the entity you intend to update and apply validated changes to it, or deliberately attach and update an entity using an approach suited to the application. A read-oriented projection is not, by itself, an update path.

Which parts of an EF Core projection run in SQL?

EF Core attempts to evaluate queries on the server. Its client versus server evaluation guidance permits client evaluation in the top-level projection in supported cases. If an expression cannot be translated elsewhere in the query, EF Core throws a runtime exception. When top-level projection work runs on the client, the query may still need to retrieve data for that work; an arbitrary C# helper is not automatically converted to SQL.

  • Prefer provider-translatable expressions for filtering, sorting, and limiting before materialization.
  • Use projection to make the intended result shape clear.
  • Inspect generated SQL for important queries, especially when you depend on column selection or provider-specific translation.

Translation behavior can vary by provider and EF Core version, so verify it against the version and provider used by the application.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.