views:

97

answers:

4

My company has just started using LINQ and I still am having a little trouble with the abstractness (if thats a word) of the LINQ command and the SQL, my question is

  Dim query = (From o In data.Addresses _
                    Select o.Name).Count

In the above in my mind, the SQL is returning all rows and the does a count on the number rows in the IQueryable result, so I would be better with

    Dim lstring = Aggregate o In data.Addresses _
    Into Count()

Or am I over thinking the way LINQ works ? Using VB Express at home so I can't see the actual SQL that is being sent to the database (I think) as I don't have access to the SQL profiler

+2  A: 

I'm not familiar with Visual Basic, but based on

http://msdn.microsoft.com/en-us/library/bb546138.aspx

Those two approaches are the same. One uses method syntax and the other uses query syntax.

You can find out for sure by using SQL Profiler as the queries run.

PS - The "point" of LINQ is you can easily do query operations without leaving code/VB-land.

Mark Canlas
Cheers but from that I can't see the it talks about .Count, using express at the moment so don't have access to the profiler (must spend money) .. PS I got your PS, should have said "Or am I not understanding the way LINQ Works"
spacemonkeys
A: 

I think you are missing the point as Linq with SQL has late binding the search is done when you need it so when you say I need the count number then a Query is created.

Before that Linq for SQL creates Expression trees that will be "translated" in to SQL when you need it....

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx http://msdn.microsoft.com/en-us/netframework/aa904594.aspx

How to debug see Scott http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx alt text

salgo60
Does that answer the above question ?
spacemonkeys
Sorry, the reason that I ask the question is that when I put the dubugger or .string on "Dim query = (From o In data.Addresses Select o.Name).Count" I get 2 which is the result, when I do the same for the other query I get the expected "select count(*)" sql, so I am leaning to the latter being better .... but as I can't access the profiler, I can't determine
spacemonkeys
Try use the logging on the context object ....http://www.codeproject.com/KB/linq/LINQ2Log4Net.aspx
salgo60
Best tool to learn is LinqPad http://www.linqpad.net/or use the debug window var sw = new System.IO.StringWriter(); db.Log = sw;see http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers
salgo60
Another stackflow entry that discuss Counthttp://stackoverflow.com/questions/1651301/c-count-extension-method-performance
salgo60
http://mattberseth.com/blog/2008/01/profiling_linq_to_sql_using_th.html
salgo60
+3  A: 

As mentioned, these are functionally equivalent, one just uses query syntax.

As mentioned in my comment, if you evaluate the following as a VB Statement(s) in LINQPad:

Dim lstring = Aggregate o In Test _
    Into Count()

You get this in the generated SQL output window:

SELECT COUNT(*) AS [value]
FROM [Test] AS [t0]

Which is the same as the following VB LINQ expression as evaluated:

(From o In Test_
    Select o.Symbol).Count

You get the exact same result.

Marc Bollinger
LINQPad is your friend.
Eric King
Thank LINQPAd has been a bit of a revelation in understanding how Ling works un the hood ... cheers
spacemonkeys
+1  A: 

An important thing here, is that the code you give will work with a wide variety of data sources. It will hopefully do so in a very efficient way, though that can't be fully guaranteed. It certainly will be done in an efficient way with a SQL source (being converted into a SELECT COUNT(*) SQL query. It will be done efficiently if the source was an in-memory collection (it gets converted to calling the Count property). It isn't done very efficiently if the source is an enumerable that is not a collection (in this case it does read everything and count as it goes), but in that case there really isn't a more efficient way of doing this.

In each case it has done the same conceptual operation, in the most efficient manner possible, without you having to worry about the details. No big deal with counting, but a bigger deal in more complex cases.

To a certain extent, you are right when you say "in my mind, the SQL is returning all rows and the does a count on the number rows". Conceptually that is what is happening in that query, but the implementation may differ. Compare with how the real query in SQL may not match the literal interpretation of the SQL command, to allow the most efficient approach to be picked.

Jon Hanna
Thanks Jon, I think the one thing that I have learned in the last month or so of using LinQ is not to presume what the SQL being produced is (and to keep away from none iQueryable object types as long as possible)
spacemonkeys
Yep. Indeed, sometimes when I do look at the SQL produced and it seems a bit strange, then going to the next level down and looking at the query plan shows that actually, the LINQ produced SQL is better than the more obvious approach. It is worth looking at the SQL produced, esp. for more complex or frequently hit queries, as you may be able to help it with appropriate indexing and statistics.
Jon Hanna