📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you reuse a projection expression across several EF Core queries without breaking translation?

Mid-Level Theory Intermediate

🎯 Key points

  • static Expression<Func<TEntity,TDto>> field
  • Pass to Select directly; still only projected columns in SQL
  • One definition removes copy-pasted projections
  • A C# method call is opaque to EF Core
  • LinqKit Expand for nested expressions
  • Check with ToQueryString()

⏱ 30-second answer

Store it as a static Expression<Func<Order, OrderListItem>> and pass it to Select. EF Core inlines the expression tree and translates it, so every query that reuses it still selects only the projected columns, and the DTO shape is defined once instead of copied into each query. A normal method (o => ToDto(o)) won't translate except as final client evaluation, which loads whole entities. Being static, the expression is built once with no per-call cost. For nested reuse, libraries like LinqKit (Invoke/Expand) or a mapper's ProjectTo help.

🔍 What the interviewer is checking

Checks practical projection reuse and expression trees.

↩ Usually asked after

More in Querying

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me