I have this function:
public async Task<string> EagerLoadAllAsync<T>(params Expression<Func<T, object>>[] includeProperties) where T : class
{
var entities = await _repository.EagerLoadAllAsync(includeProperties);
entities.ForEach(l =>
{
var lead = l as Lead;
if (lead.User != null)
{
// We must reduce the amount of data being sent to the client side
lead.User = new Domain.Identities.ApplicationUser { FirstName = lead.User.FirstName, LastName = lead.User.LastName, UserName = lead.User.UserName };
}
});
var json = await Task.Factory.StartNew(() => JsonConvert.SerializeObject(entities, Formatting.Indented, new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}));
return json;
}
I think it is still async
it has to awaitable
functions running, one to fetch the data from the database and the other to convert it json
.
I was wondering if the ForEach
loop in the middle sort of destroys the async
from the ground up approach?
Is there a way to make this more async
?
Should it be more async
?
Before I needed to reduce the data being sent to the client I had this function:
public async Task<string> EagerLoadAllAsync<T>(params Expression<Func<T, object>>[] includeProperties) where T : class
{
var json = await Task.Factory.StartNew(async() => JsonConvert.SerializeObject(await await _repository.EagerLoadAllAsync(includeProperties), Formatting.Indented, new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}));
return json.Result;
}