Reputation: 1592
So when I write a query for a DbContext does the order of the queryable actually represent the output query. For example:
_dbContext
.Table
.OrderBy(i => i.Date)
.Take(25)
.Where(i => i.Variable == "something")
.ToList()
versus
_dbContext
.Table
.Where(i => i.Variable == "something")
.OrderBy(i => i.Date)
.Take(25)
.ToList()
So these queries are different because the first takes the last 25 items by date and performs a where clause. But the other one takes 25 from the result of the where.
Will this stay in this order when it get's executed or is this like a builder where all properties are set and executed.
If I look at plain SQL there is no way for me to TAKE
before the WHERE
in the same query. So it makes sense to me that is wouldn't.
The reason for my confusion is that if I would write the first query in MS SQL we would get this:
SELECT TOP 25 * FROM `Table` WHERE `Variable` = 'something' ORDER BY `Date`
Which takes 25 from the where result.
Upvotes: 2
Views: 592
Reputation: 6162
If you enable logging SQL, you will see that order matters for these two queries
SELECT [t].[Id], [t].[Date], [t].[Variable]
FROM (
SELECT TOP(@__p_0) [m].[Id], [m].[Date], [m].[Variable]
FROM [MyObjects] AS [m]
ORDER BY [m].[Date]
) AS [t]
WHERE [t].[Variable] = N'something'
ORDER BY [t].[Date]
and
SELECT TOP(@__p_0) [m].[Id], [m].[Date], [m].[Variable]
FROM [MyObjects] AS [m]
WHERE [m].[Variable] = N'something'
ORDER BY [m].[Date]
Logging can be enabled by setting log level for EntityFramework Core in appsettings.json
{
"Logging": {
"LogLevel": {
"Microsoft.EntityFrameworkCore": "Debug"
}
}
}
Also you need to make sure your db context has this specific constructor overload
public AppDbContext(DbContextOptions options) : base(options)
{
}
Upvotes: 5