If I need generate a fairly large dataset using LINQ and it may take a while (say a few seconds) and I need to (would like to) generate feedback to the use as to %'age done, is there an easy/ preferred way to do this?
Example, say I have list A with 1000 cars and list B with 1000 trucks and I want to select all possible ordered (car, truck) pairs where car.color == truck.color link this:
var pairs = from car in A
from truck in B
where car.color==truck.color
select new {car, truck};
Now at some point this will be evaluated as a set of nested foreach loops. I would like to be able to report %'age complete as it interates and ideally update a progressbar or something.
EDIT: Just after my query, I store the result in a member variable as a list like this (which forces the query to execute):
mPairs = pairs.ToList();
I do this because I am executing this in a background worker thread as I do not want the UI thread to freeze up as it evaluates the LINQ expression on demand on the UI thread (this is in Silverlight BTW). Hence why I would like to report progress. The UX is basically this:
- A user drags an item onto the workspace
- The engine then kicks up on a background thread to determine the (many) connection possibilities to all of the other items on the workspace.
- While the engine is calculating the UI does not allow new connections AND reports progress to indicate when the new item will be "connectable" to the other items (all the possible connection paths not already in use have been determined via LINQ).
- When the engine completes the calculation (query), the item is connectable in the UI and the possible connection paths are stored in a local variable for future use (e.g. when the user clicks to connect the item all the possible paths will be highlighted based upon what was calculated when it was added)
(a similar process must happen on deletion of an item)