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.
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.