Friday, 3 March 2017

c# - Why IEnumerable method call the database without condition?

I have these method in my Generic Repository :



public IQueryable Query()
{
return _dbSet.AsQueryable();

}

public IEnumerable 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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...