LINQ Interview Question #113

How can indexes affect LINQ query performance?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

LINQ itself does not create database indexes, but the SQL generated from LINQ benefits from appropriate indexes on columns used for selective filtering, joining, and ordering.

Detailed Explanation

LINQ itself does not create database indexes, but the SQL generated from LINQ benefits from appropriate indexes on columns used for selective filtering, joining, and ordering.

A syntactically concise LINQ query can still be slow if the resulting SQL scans large tables. Performance analysis should consider generated SQL, execution plans, data distribution, and index design.

Code Example

var orders = await db.Orders
    .Where(o => o.CustomerId == customerId)
    .OrderByDescending(o => o.CreatedAt)
    .Take(20)
    .ToListAsync();

// Database indexes should support important access patterns.