Files
freeCodeCamp/guide/english/csharp/linq/count/index.md
2018-10-16 21:32:40 +05:30

25 lines
959 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Count
---
# Any
Returns a count of elements in the sequence that satisfy a condition.
### Signature
```csharp
Enumerable.Count<TSource> Method (IEnumerable<TSource>,Func<TSource,Boolean>)
```
## Example
```csharp
var fruits = new List<Fruit>() {
new Fruit() { Id = 1, Name = "Orange", Color = "Orange", Quantity: 3 },
new Fruit() { Id = 2, Name = "Strawberry", Color = "Red", Quantity: 12 },
new Fruit() { Id = 3, Name = "Grape", Color = "Purple", Quantity: 25 },
new Fruit() { Id = 4, Name = "Pineapple", Color = "Yellow", Quantity: 1 },
new Fruit() { Id = 5, Name = "Apple", Color = "Red", Quantity: 5 },
new Fruit() { Id = 6, Name = "Mango", Color = "Yellow", Quantity: 2 }
};
// Count of Fruit with Color Red.
var yellowCount = fruits.Count(f => f.Color == "Red"); // 2
// Count of Fruit with Quantity > 2
var quantityOverTwo = fruits.Count(f => f.Quantity > 2); // 4
```