Again:
If I could teach people about LINQ, it would be that the value of the query is the object that executes the query, not the results of the query .
You create a request once, creating an object that can execute the request. Then you execute the request twice. Unfortunately, you created a query that not only calculates the value, but also produces a side effect, and therefore, executing the query twice causes a side effect twice. Do not use reusable request objects that create side effects, ever . Requests are a mechanism for raising questions, hence their name. They are not intended for the flow control mechanism, but for what you use them for.
Executing a query twice leads to two different results, because, of course, the query results can be changed between two executions. If the query queries the database, let's say the database could be changed between execution. If your request is "what are the names of all customers in London?" the answer may change from millisecond to millisecond, but the question remains the same. Always remember, the query is a question.
I would be inclined to write something without asking. Use the "foreach" loops to create side effects.
public async Task<Dictionary<string, object>> Read(IEnumerable<string> queries) { var tasks = new Dictionary<string, Task<object>>(); foreach (string query in queries) tasks.Add(query, LoadDataAsync(query)); await Task.WhenAll(tasks.Values); return tasks.ToDictionary(x => x.Key, x => x.Value.Result); }
source share