Partial Return Method

I am developing a method that looks for information in a very large table. Since I don’t use ORDER BY or anything special in the query (just SELECT id,description FROM complain WHERE description like 'YOUR TEXT HERE'" ), I was hoping to provide a more dynamic user interface by returning batches of results. This would be like doing query in Management Studio.

A few details, my call stack is small, but not all in one method. There buttonSearchClick , performCleanSearch and searchComplainBasedOnDetailInfo each of them is in a different layer (Interface, SearchBLL and SearchDAL, respectively).

I was thinking of creating an async method that populates something like List<Complain> , but that doesn't seem clean. I would have to make 3 layers asynchronous. Does anyone have any better ideas on how to implement this? Or is this the best way to do this?

Edit1: I managed to use SqlCommand.BeginExecuteReader along with Async processing in the connection string to get the query results as they appear ... now I need to figure out a way for my DAL method to be asynchronous, so the top level can get the results async too ... I I thought about implementing some kind of buffer ... maybe it’s the turn ...

Edit2: I am not looking for a paging solution or twitter similar to one (where you are viewing and viewing new results), because I know that the user will have to read all the information received ...

+4
source share
2 answers

You can use BackgroundWorker and get your batches in your DoWork doing something like:

 DataTable dt; int iRecords = 0; do { dt = new DataTable(); using(SqlConnection con = new SqlConnection("")) { SqlCommand cmd = new SqlCommand(string.Format("SELECT TOP 100 * FROM complain where ID > {0}", iRecords)); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); //Report your progress here } } while(dt.Rows.Count != 0) 
+1
source

You can return IEnumerable<Task<IEnumerable<T>>> (T will be configured for any type of your elements) from your method. Each Task<IEnumerable<T>> will be a package of information potentially received in the future. The method itself can either be an iterator block, which gives each batch as it is received, or depending on your request method, you can execute a batch sequence without having to write an iterator block.

Then, from the perspective of the caller, they can write something like this:

 foreach(Task<IEnumerable<T> batch in GetBatches()) { updateUIWithBatch(await batch); } 
0
source

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


All Articles