views:

740

answers:

1

Hi, 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)
+6  A: 

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!

RobS
+1 for the T-SQL generated by linq2sql being superior to EFs T-SQL :) (not enough people are saying that out loud), l2s won't die as long as we (the developers) want to keept it alive...
Pop Catalin
The EF's T-SQL is so bad - I blogged a sample on the weekend: http://internationalized.spaces.live.com/blog/cns!43F3A7682D1564E4!1458.entry
RobS
Just a FYI - there are some cases where the order matters. If you for example call .OrderBy... before .Where on an IQueryable then you can end up with SQL queries that are either inefficient or does not do what you expect them to. But other than that it is safe to juggle around IQueryables...
KristoferA - Huagati.com
Yeah this is true, though hopefully people are profiling their queries :)
RobS
Rob: Isn't the point of Linq 2 SQL that you shouldn't have to profile queries unless they're particularly slow? This seems to be a frequent problem with Microsoft API's... an API is supposed to provide some level of abstraction, but with Microsoft's you always very quickly hit a point where you need to know exactly what's going on inside (ala Raymond Chan) to get anything working. Microsoft leaks abstraction like a sieve.
evilfred
That is true, but keep in mind that Microsoft hasn't got a whole lot of control over how people compose their queries, especially when they provide a certain level of flexibility. In general, profiling shouldn't be necessary except for large or complex queries.
RobS