private IEnumerable<string> Tables
{
get
{
yield return "Foo";
yield return "Bar";
}
}
假设我想要迭代这些并写一些类似于processing #n of #m的东西。
有没有一种方法可以让我在不进行主迭代的情况下求出m的值?
我希望我讲清楚了。
private IEnumerable<string> Tables
{
get
{
yield return "Foo";
yield return "Bar";
}
}
假设我想要迭代这些并写一些类似于processing #n of #m的东西。
有没有一种方法可以让我在不进行主迭代的情况下求出m的值?
我希望我讲清楚了。
当前回答
IEnumerable<T>上的System.Linq.Enumerable.Count扩展方法有以下实现:
ICollection<T> c = source as ICollection<TSource>;
if (c != null)
return c.Count;
int result = 0;
using (IEnumerator<T> enumerator = source.GetEnumerator())
{
while (enumerator.MoveNext())
result++;
}
return result;
因此,它尝试强制转换为具有Count属性的ICollection<T>,并尽可能使用该属性。否则它会迭代。
因此,最好的方法是在IEnumerable<T>对象上使用Count()扩展方法,因为这样可以获得最好的性能。
其他回答
LINQ中有一个用于。net 6的新方法 观看https://www.youtube.com/watch?v=sIXKpyhxHR8
Tables.TryGetNonEnumeratedCount(out var count)
简化所有答案。
IEnumerable没有Count函数或属性。为了得到这个,你可以存储count变量(例如,使用foreach)或使用Linq来获得count。
如果你有:
IEnumerable < >产品
然后:
声明:"using System.Linq;"
数:
.Count products.ToList ()
不,不一般。使用枚举对象的一点是,枚举中的实际对象集是不知道的(预先知道,甚至根本不知道)。
我建议你打电话给ToList。是的,您正在提前进行枚举,但您仍然可以访问项目列表。
除了你的直接问题(已经得到了否定的回答),如果你想在处理一个可枚举对象时报告进度,你可能想看看我的博客文章《在Linq查询期间报告进度》。
它让你这样做:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += (sender, e) =>
{
// pretend we have a collection of
// items to process
var items = 1.To(1000);
items
.WithProgressReporting(progress => worker.ReportProgress(progress))
.ForEach(item => Thread.Sleep(10)); // simulate some real work
};