IEnumerable vs IQueryable in C#
- If you’re learning C#, this confusion is normal
- Think of shopping for groceries
IEnumerable
- You go to the store, buy all items, bring them home
- Then you decide what to keep and what to ignore.
- Data is fetched first, filtering happens in memory
- Example: C# var data = users.Where(u => u.IsActive);
IQueryable
- You tell the shopkeeper exactly what you want
- Only those items are brought to you.
- Filtering happens in the database
- Example: C# var data = context.Users.Where(u => u.IsActive);
Key difference
- IEnumerable → Data comes first, then filtering
- IQueryable → Filtering first, then data
Rule of thumb:
- Small data? → IEnumerable
- Large database data? → IQueryable