Return IEnumerable<T> or IQueryable<T> in C#: what's the difference and what to choose?
Hi everyone! I'm a beginner C# developer working with Entity Framework and LINQ. I often see in my colleagues' code that they return either `IEnumerable` or `IQueryable` from methods. At first I thought they were interchangeable, but then I noticed some strange behavior.
Here's an example. I have a method that gets a list of orders:
```csharp
public IEnumerable GetOrders()
{
using (var db = new MyDbContext())
{
return db.Orders.Where(o => o.IsActive);
}
}
```
Then I call it and apply additional filtering:
```csharp
var orders = GetOrders().Where(o => o.Total > 100).ToList();
```
I expected that one SQL query with two conditions (`IsActive = true AND Total > 100`) would be sent to the database, but the profiler shows that first all active orders are selected, and then...
No answers yet.
Log in to leave an answer