The best way to get a count of IEnumerable

前端 未结 11 1407
悲哀的现实
悲哀的现实 2020-12-14 15:47

Whats the best/easiest way to obtain a count of items within an IEnumerable collection without enumerating over all of the items in the collection?

Possible with LIN

11条回答
  •  天命终不由人
    2020-12-14 16:09

    It also depends on what you want to achieve by counting.. If you are interested to find if the enumerable collection has any elements, you could use

    myEnumerable.Any() over myEnumerable.Count() where the former will yield the first element and the later will yield all the elements.

提交回复
热议问题