Reputation: 2416
I know Linq-to-SQL is dead, but anyway, I think this is really basic, I'm just confused about what magic Linq-to-SQL does and doesn't do with regards to the SQL it generates.
If I have built up an Expression tree as "myPredicate", and have something like this:
(from request in DataContext.RequestsTable
select request).Where(myPredicate)
.OrderByDescending(item => item.changeDate)
.Take(10)
is it going to work like the following SQL:
SELECT TOP 10 * FROM RequestsTable
WHERE (<<myPredicate equivalent>>)
ORDER BY ChangeDate DESC
It just seems weird to me because the ".Where()" comes after the "select" in my example code. Does the relative positioning of the "select" and "where()" and "orderby()" affect things?
Alternatively, could I do it all in sql-esque syntax? For example, is there some way to use my WHERE predicate in the alternative syntax, something like this?
(from request in DataContext.RequestsTable
where [somehow inject myPredicate]
order by changeDate descending
select request).Take(10)
Upvotes: 2
Views: 1845
Reputation: 9422
You've got the same query there, LINQ to SQL won't evaluate and generate the T-SQL until after you've done something to execute the query (such as a .ToList() for example. The ordering doesn't matter.
In fact, you can even add OrderBy and Where clauses after the assignment of the initial query.
e.g
var query = (from x in context.RequestsTable
select x);
query = query.AsQueryable().Where(<>);
return query.ToList(); //executes
is the same as:
return (from x in context.RequestsTable
where <>
select x).ToList(); //executes
is the same as:
return (from x in context.RequestsTable
selext x).Where(<>).ToList();
I'm not sure LINQ to SQL is "dead" however I have heard that it might be being rolled into the ADO Entity Framework. LINQ to SQL's generated T-SQL is far superior to the Entity Framework's!
Upvotes: 7