I have these method in my Generic Repository :
public IQueryable<T> Query()
{
return _dbSet.AsQueryable();
}
public IEnumerable<T> Enum()
{
return _dbSet.AsEnumerable();
}
If I call these method in my service layer
_repo.Query().Where(w => w.IsActive == true);
_repo.Enum().Where(w => w.IsActive == true);
The Queryable method will call this TSQL Syntax in the database, profiler below :
SELECT
[Extent1].[ProjectID] AS [ProjectID],
[Extent1].[Name] AS [Name],
[Extent1].[IsActivity] AS [IsActivity],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[Project] AS [Extent1]
WHERE 1 = [Extent1].[IsActive]
which is expected, but the Enumerable method will select everything in the database without where condition, profiler below :
SELECT
[Extent1].[ProjectID] AS [ProjectID],
[Extent1].[Name] AS [Name],
[Extent1].[IsActivity] AS [IsActivity],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[Project] AS [Extent1]
It means that the Enumerable will greedily call every record in the database including IsActive = 0
record. Why is this happening? I thought IEnumerable is suppose to be deffered which means it won't do anything until something like ToList()
is called.
Any help will be appreciated and apologize for bad english.