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

📚 Interview question

How do you map and call a SQL Server table-valued function from EF Core so it composes with LINQ?

Mid-Level Theory Intermediate

🎯 Key points

  • Inline TVF (single SELECT) is expanded into the outer query by the optimiser
  • DbContext method returning IQueryable<T> via FromExpression
  • HasDbFunction registration in OnModelCreating (EF Core 5+)
  • Composes with Where, OrderBy, Include in one SQL statement
  • Multi-statement TVFs allow procedural logic but are opaque, with weaker cardinality estimates
  • Parameters are passed as SQL parameters, so it stays injection-safe

⏱ 30-second answer

Create an inline table-valued function in SQL Server, add a method on the DbContext that returns IQueryable<T> via FromExpression(() => Fn(arg)), and register it with modelBuilder.HasDbFunction (supported since EF Core 5). EF Core then translates db.Fn(x).Where(...).Include(...) into SELECT ... FROM dbo.Fn(@p0) WHERE .... Inline TVFs are a single SELECT, so the optimiser expands them into the outer query like a parameterised view and uses real statistics. Multi-statement TVFs allow procedural logic (table variables, loops, several statements) when you genuinely need it, but they're opaque to the optimiser: SQL Server 2017+ interleaved execution helps, yet their row estimates are often poor, so they're usually slower when composed with large queries.

🔍 What the interviewer is checking

Checks the composable alternative to stored procedures.

↩ Usually asked after

More in Advanced EF Core

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me