Linq on Linq without database roundtrip: speed issue
I have a speed problem in the following situation:
The SQL Server DB is hosted in a shared hosting environment, accessed through DSL 2MBytes line
There is a table of competition results which holds some 32 rows per competition.
When I display the result rows for a given competition in a datagridview, I use LINQ to produce the dgv datasou开发者_JAVA技巧rce, and THEN I add an extra computed column.
Why then adding a column? **Because the value of Row1.extraColumn depends on the value of Row1.Result compared to Row2.result**
I wrote:
dim mainQuery = from c in competResults
where competID = xxx
select c.resultID, c.resultPosition, c.result
dataGridView1.datasource = mainQuery.ToList
'Prepare and add Exrta Column
dataGridView1.ADD Extra Column
Remember the dgv have only some 32 rows... Only...
Using LINQ I have a subquery like this:
dim rowA = from q in mainQuery where rowNumber = xxx select q
dim rowB = from q in mainQuery where rowNumber = yyy select q
Perform the calculation and write the result within dgv, like:
for each R in dgv.rows
if R.cells("rowNumber").value = rowA.RowNumber then R.cells("ExtraColumn)).value = some result
if R.cells("rowNumber").value = rowB.Rownumber then R.cells("ExtraColumn)).value = some result
next
And THAT takes some 30 seconds to 1 minute
I thought LINQ wouldn't go back to query the DB when querying a query whose results has been searched (When I performed the dataGridView1.datasource = CResults.ToList).
Of course, I can do the job iterating through the DGV rows, but I was more like using LINQ...
Any hint welcome...
To make sure your query is executed once only and mainQuery
is local, change your first query to:
dim mainQuery = (from c in competResults
where competID = xxx
select c.resultID, c.resultPosition, c.result).ToList()
精彩评论