-1

I've been playing around with LINQ to SQL and I just have a couple of simple questions:

  • When do I need Select on the end of my query?
  • When can I omit the Select?

Here are my example queries:

Dim pageRoute = From r In db.PageRoutes Where r.PageId = pageId Order By r.Id Descending

Dim pageRoute = From r In db.PageRoutes Where r.PageId = pageId Order By r.Id Descending

Dim dp = From r In db.DownloadPageOnlineOnlies Where r.PageId = pageId Order By r.Weight Descending, r.Id Ascending

Dim download = (From r In db.Downloads Where r.Id = id).First
  • Are any of them technically wrong?
  • Could they be improved with a Select or something else?

In a nutshell, I don't understand when I would need either:

Select r
Select r.AColumnINeed, r.BColumnINeed (does this improve performance?)

Thanks.

P.S. I like to write my LINQ queries on one line unless they are really big.

jrummell
  • 42,637
  • 17
  • 112
  • 171
Chris Cannon
  • 1,157
  • 5
  • 15
  • 36
  • 1
    Isn't the `Select` always optional in VB.NET? http://stackoverflow.com/a/6515130/284240 – Tim Schmelter May 30 '12 at 16:56
  • I don't know that's why I'm asking. If something is optional then I usually leave it out just to give my fingers a rest! – Chris Cannon May 30 '12 at 16:57
  • In http://stackoverflow.com/questions/6515037/differences-in-linq-syntax-between-vb-net-and-c-sharp/6515130#6515130 what does it mean by "select what is current"? – Chris Cannon May 30 '12 at 17:15
  • 1
    If you don't specify a select, *all* is selected. `Dim q = From r In tbl.AsEnumerable From c In tbl.Columns.Cast(Of DataColumn)()` returns an anonymous type with a cartesian product of all DataRows and all DataColumns in that DataTable. Always prefer readability, so use `Select` whenever it's unclear what the query returns. – Tim Schmelter May 30 '12 at 17:23
  • Ok so the query would be along the lines of `Dim q = From r In tbl.AsEnumerable From c In tbl.Columns.Cast(Of DataColumn)() Select r.Column1, c.Column2` ? – Chris Cannon May 30 '12 at 17:34
  • What query? Query what you need. So if you need an anonymous type of field1 and field2(you can omit the `AsEnumerable` in VB also): `Dim q = From r In tbl Select New With {.Col1 = r.Field(Of String)("Column1"), .Col2 = r.Field(Of String)("Column2")}` – Tim Schmelter May 30 '12 at 17:45
  • Ok I think I need to do some more LINQ reading at some point. Thanks for your help anyway. – Chris Cannon May 30 '12 at 17:48

3 Answers3

2

The select portion of the linq statement is completely optional if and only if you want to get a full object out of the collection that matches your where clause. If you want individual value(s) from an object in the collection being LINQ'd through then you need to use a select clause.

I personally always put the select r clause on the end just out of pure habit but I have come across a few issues with other peoples code when I have option strict on and they did not when they wrote the LINQ. Leaving the select clause off yields multiple late binding errors if you decide to turn option strict on in the future for whatever reason.

so in short you don't need the select clause but you are only helping yourself later on down the line if you decide to turn option strict on. And it does make your code much more readable in my opinion.

Mike_OBrien
  • 1,395
  • 15
  • 34
1

Let's have a table with 20 columns. Take a query WITH select (2 columns) and one WITHOUT. The execution plan of the two can be different and there's much less data to transfer from the database server in the former case.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
-1

it a good practice to use select plus if you have a query that requests a more precise result and like you only want a or a and b from you query you might use select and it saves the memory allocation for your query since it know exact number of variables you are going to return.

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52