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的值?
我希望我讲清楚了。
当前回答
我使用这样的代码,如果我有字符串列表:
((IList<string>)Table).Count
其他回答
我使用这样的代码,如果我有字符串列表:
((IList<string>)Table).Count
我认为这是最简单的方法
Enumerable.Count<TSource>(IEnumerable<TSource> source)
参考:system.linq.enumerable
简化所有答案。
IEnumerable没有Count函数或属性。为了得到这个,你可以存储count变量(例如,使用foreach)或使用Linq来获得count。
如果你有:
IEnumerable < >产品
然后:
声明:"using System.Linq;"
数:
.Count products.ToList ()
你也可以这样做:
Tables.ToList<string>().Count;
IEnumerable不支持这个。这是有意为之。IEnumerable使用惰性求值在需要元素之前获取它们。
如果你想知道项目的数量而不迭代它们,你可以使用ICollection<T>,它有一个Count属性。