I'm having trouble understanding why the SQL output has a sub-query for a simple query I wrote in LINQ. This is my code:
var list = db.User.Where(u => u.Name == somename).OrderBy(u => u.IdUser).ToList();
where somename is a parameter I'm passing at execution time.
The output SQL is:
SELECT
Project1.IdUser,
Project1.Name
FROM (SELECT
Extent1.IdUser,
Extent1.Name
FROM user AS Extent1
WHERE Extent1.Name = 'John' /* @p__linq__0 */) AS Project1
ORDER BY
Project1.IdUser ASC
Should the output really have a sub-query for something that simple?
I also tried
var list = db.User.Where(u => u.Name.Equals(somename)).OrderBy(u => u.IdUser).ToList();
which generates the same output as above.
If I hard code the parameter, like:
var list = db.User.Where(u => u.Name == "John").OrderBy(u => u.IdUser).ToList();
It works as expected, generating only
SELECT
Extent1.IdUser,
Extent1.Name
FROM user AS Extent1
WHERE 'John' /* @gp1 */ = Extent1.Name
ORDER BY
Extent1.IdUser ASC
A few things I'm using:
I'm not a LINQ expert, so what am I missing here?
An ORDER BY command cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery.
No ORDER BY is valid in a subquery when you are interested in a subset of the overall data, hence you always need a TOP (SQL Server). There's no point having an ORDER BY without TOP in a subquery because the overall ordering of the results is handled by the outer query.
Count() methodIEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" }; // Will return 4 int result = strings. Count(); NOTE: The Count() LINQ method (an extension method to IEnumerable<T> ) is slightly different from the Count property on List<T> .
In SQL, it's possible to place a SQL query inside another query known as subquery. For example, SELECT * FROM Customers WHERE age = ( SELECT MIN(age) FROM Customers ); Run Code. In a subquery, the outer query's result is dependent on the result-set of the inner subquery.
As other pointed, the query results in same execution plan as yours. Entity Framework (and LINQ to Entites) is here to help you avoid writing SQL and bothering about SQL (to some extent). Under normal circumstances you don't care about SQL being generated nor you "debug" it. You just care whether the LINQ query is correct. Entity Framework (should) translates it into correct (sometimes even expected) SQL (and again, execution plan matters).
I'm not saying that you shouldn't look at SQL for performance reasons (or better to say execution plan of that query). But that should be done after you identified performance problems. And you should try to write queries simple first, that's the way to success. Of course if you know SQL you know this world of sets is different from world of object - you can write easily fairly average query in LINQ (thanks to objects world), but this will end up as nasty SQL (sets world) because of "mismatch" between worlds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With