Using MySQl 5.5.19, entity framework 4.3, and have tried both the Sun-provided MySQL connector and the DevArt one without any difference between the two.
So, I’ve got two statements which produce different queries. The first is:
var desserts = context.desserts
.AsQueryable()
.OrderBy(m => m.Id)
.Where(m => m.FlavorId == 123)
.ToList();
and the second is
var desserts = context.desserts
.AsQueryable()
.OrderBy(m => m.Id)
.Where(m => m.FlavorId == someFlavorId)
.ToList();
The first query produces a SQL query that looks something like this:
SELECT
Extent1.Id,
Extent1.Name,
Extent1.FlavorId
FROM icecream.dessert AS Extent1
WHERE Extent1.FlavorId = 123
ORDER BY Extent1.Id ASC
The second one produces a much more costly query that looks something like this:
SELECT
Project1.Id,
Project1.Name,
Project1.FlavorId
FROM(
SELECT
Extent1.Id,
Extent1.Name,
Extent1.FlavorId
FROM icecream.dessert AS Extent1
WHERE Extent1.FlavorId = 123
) AS Project1
ORDER BY Project1.Id ASC
And, of course, the latter query is the one that is used in my production code. I’m confused why these two result in a different query.
Any ideas how to fix this?
They produce different sql, because the first query you are passing in a constant and the second one you are passing in a parameter.
If you want to mimic the first query you would need to create an Expression for the Where predicate like this: