When does delayed execution occur?

I have a situation where I want to get data from a database and assign it to the tooltip of each row in a ListView control in WPF. (I use C # 4.0.) Since I did not do this before, I started using a smaller, simpler application to bring ideas to before trying to use them in my main WPF application.

One of my problems is the amount of data that could potentially decrease. For this reason, I thought I would use LINQ to SQL, which uses deferred execution. I thought this would help and not pull out the data until the user types his mouse over the corresponding line. For this, I'm going to use a separate function to assign values ​​to the tooltip from the database, passed to the parameters that I need to pass to the corresponding stored procedures. I execute 2 queries using LINQ to SQL, using 2 different stored procedures and assigning the results to two different DataGrids.

Although I know that LINQ to SQL uses deferred execution, I'm starting to wonder if some of the code I'm writing can succeed in using LINQ to SQL. For example, when testing in my simpler application, I select several different values ​​to see how it works. One choice of values ​​did not result in data return, since there was no data for these parameters. I thought this might lead to user confusion, so I decided to check the Count property from the list that I assigned, from running the DBML-related method (associated with the stored procedure). Thinking about this, I would think that LINQ would have to run a query to give me the result for the Count property. I am wrong?

If I excluded the call to the Count property, I still wonder if I have a problem; if LINQ can still be called because I am attaching a tooltip to the control through a function call?

+3
source share
3 answers

You should use Any(), and not Count(), but even Any()lead to the execution of the query - in the end, it cannot determine if there are any rows in the result set without executing the query. But there the query is executed and the result set is retrieved. Any()will extract one row, Count()will extract them all.

, , , , - . Outlook, , "". , , , , . , , , , , , , . . .

. BackgroundWorker, DataTable, DataTable RunWorkerCompleted. ( , , UI, .) ToolTip , (, null, - " ..." ), DataTable, , , . . , - , .

+1

, Count, . , , LINQ, , , DataGrids, .

EDIT: , - , , . Count , LINQ to SQL, Any(). ( Any(), , , Count > 0)

+2

, Count() Any() LINQ, . , , , , , / . . dev , . DisplayTooltip(), LINQ. , , .

+1

Source: https://habr.com/ru/post/1768500/


All Articles