考虑以下代码块:
using (FileStream fs = new FileStream(@"C:\bad_records.txt",
FileMode.Create,
FileAccess.Write))
{
var badEnumerable = _cache.Where(kvp => !kvp.Value.Item1);
fs.WriteLine(string.Format("BAD RECORDS ({0})", badEnumerable.Count()));
fs.WriteLine("==========");
foreach (var item in badEnumerable)
{
fs.WriteLine(string.Format("{0}: {1}", item.Key, item.Value.Item2));
}
}
where_cache
定义如下:
static Dictionary<string, Tuple<bool, string, string>> _cache;
我是否将这个可枚举迭代了两次?一次与Count()
一次与foreach
?