【发布时间】:2015-02-12 18:22:40
【问题描述】:
我有这个功能:
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;
}
我认为它仍然是 async 它必须运行 awaitable 函数,一个从数据库中获取数据,另一个将其转换为 json。
我想知道中间的ForEach 循环是否会从根本上破坏async?
有没有办法让这个更async?
是不是应该更多async?
在我需要减少发送到客户端的数据之前,我有这个功能:
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;
}
【问题讨论】:
标签: c# .net asp.net-mvc asynchronous async-await