我在Generic Repository中有这些方法:
public IQueryable<T> Query()
{
return _dbSet.AsQueryable();
}
public IEnumerable<T> Enum()
{
return _dbSet.AsEnumerable();
}
如果我在服务层中调用这些方法
_repo.Query().Where(w => w.IsActive == true);
_repo.Enum().Where(w => w.IsActive == true);
Queryable方法将在数据库,下面的探查器中调用此TSQL语法:
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]
这是预期的,但是Enumerable方法将选择数据库中的所有内容而不管where condition,profiler如下:
SELECT
[Extent1].[ProjectID] AS [ProjectID],
[Extent1].[Name] AS [Name],
[Extent1].[IsActivity] AS [IsActivity],
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[Project] AS [Extent1]
这意味着Enumerable将贪婪地调用数据库中的每条记录,包括IsActive = 0
记录。为什么会这样?我认为IEnumerable被认为是被修改的,这意味着在调用ToList()
之类的东西之前它不会做任何事情。
任何帮助都会受到赞赏并为糟糕的英语道歉。