计算foreach语句中的元素数

use*_*937 0 c# foreach sum count

@foreach (var type in Model.GetPeople())
{
    var peopleGroup1 = persons.First();
    var category = new category { Id = person.CategoryId,;

    etc...
}
Run Code Online (Sandbox Code Playgroud)

如何计算foreach语句中"类型"的数量?

Chr*_*Fin 7

IEnumerable<T>有一个Count()扩展名:

var people = Model.GetPeople();
var count = people.Count();
Run Code Online (Sandbox Code Playgroud)

根据具体类型,它已经具有Count属性:

count = people.Count;
Run Code Online (Sandbox Code Playgroud)

这是适用于所有类型的实现ICollectionICollection<T>,但不适合IQueryable<T>.数组有Length属性.