Scott Hanselman has an
interesting post where he demonstrates how to create an asynchronization API on top of an existing Linq to SQL query. I donβt have time to think too much about this idea, but I suppose that you can create a more general extension method that would allow any object of type IQueryable or IEnumerable to deploy the same method.
Here is the code directly from his post to use as a link.
SqlCommand _beginFindCmd = null; public IAsyncResult BeginFind(int id, AsyncCallback callback, Object asyncState) { var query = from w in _db.Widgets where w.Id == id select w; _beginFindCmd = _db.GetCommand(query) as SqlCommand; _db.Connection.Open(); return _beginFindCmd.BeginExecuteReader(callback, asyncState, System.Data.CommandBehavior.CloseConnection); } public Widget EndFind(IAsyncResult result) { var rdr = _beginFindCmd.EndExecuteReader(result); var widget = (from w in _db.Translate<Widget>(rdr) select w).SingleOrDefault(); rdr.Close(); return widget; }
With a bit of work, this TPL could be made as well as even cleaner as a single asynchronous method. If I have a chance to do this, I will post what I came up with.
source share