我不想使用foreach
循环两次,所以我尝试使用 LINQ,但不幸的是这不起作用:
return _etags.ToDictionary(item => item.Key, async item => await item.Value);
谁能告诉我应该怎么做才能改进我的代码,或者我该如何Dictionary<string, Task<string>>
转换Dictionary<string, string>
?
这是我的代码:
private static async Task<Dictionary<string, string>> GetETagsAsync(List<string> feedsLink)
{
var eTags = new Dictionary<string, string>(feedsLink.Count);
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
var _eTags = new Dictionary<string, Task<string>>(feedsLink.Count);
eTags = new Dictionary<string, string>(feedsLink.Count);
foreach (string feedLink in feedsLink)
{
if (Uri.IsWellFormedUriString(feedLink, UriKind.Absolute))
{
_eTags.Add(feedLink, GetETagAsync(feedLink));
}
else
{
throw new FormatException();
}
}
foreach (KeyValuePair<string, Task<string>> eTag in _eTags)
{
eTags.Add(eTag.Key, await eTag.Value);
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
return eTags;
}