(using Entity Framework)
when i write :
IEnumerable<string> q = customers /*EF entity*/
.Select (c => c.Name.ToUpper())
.OrderBy (n => n)
the c# compiler knows how to emit expression trees which in turn , the sql execute :
SELECT UPPER (Name) FROM Customer ORDER BY UPPER (Name)
notice also the order by clause is there
but
i saw this link :
he wrote :
IEnumerable<employee> emp =
dc.Employees.Where(x => x.Desc.StartsWith("soft"));
emp = emp.Take(1);
after investigating the final query he saw :
SELECT [t0].[Id], [t0].[Name], [t0].[Address], [t0].[Desc] AS [Desc]
FROM [dbo].[Employee] AS [t0]
WHERE [t0].[Desc] LIKE @p0
notice there is no top clause
why is that ?
shouldn’t Take(x) be added to the query ?
will writing it like this :
IEnumerable<employee> emp =
(dc.Employees.Where(x => x.Desc.StartsWith("soft"))).Take(1);
would added the TOP clause to the query being sent to SQL ?
what is going here ?
( I already know that take is not a deferred execution)
Extension methods such as
Take()are static methods, and as such they are resolved at compile time.The compile time type of emp is
IEnumerable<employee>(because it was explicitly declared as one), so the compiler picksEnumerable.Takeinstead ofQueryable.Take, which does not perform any query translation.If you would have been lazy and would just have used
varinstead of a type name:it would have worked, because the compiler would have picked
IQueryable<employee>for emp (because the expression you initialize it with is of that type), and thusQueryable.Takefor the second call.