Skip to content
Merged
14 changes: 9 additions & 5 deletions docs/fundamentals/code-analysis/quality-rules/ca1827.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA1827: Do not use Count/LongCount when Any can be used (code analysis)"
description: "Learn about code analysis rule CA1827: Do not use Count/LongCount when Any can be used"
ms.date: 04/24/2020
ms.date: 07/17/2023
ms.topic: reference
f1_keywords:
- "DoNotUseCountWhenAnyCanBeUsedAnalyzer"
Expand All @@ -12,7 +12,7 @@ helpviewer_keywords:
author: mavasani
ms.author: mavasani
---
# CA1827: Do not use Count/LongCount when Any can be used
# CA1827: Do not use Count()/LongCount() when Any() can be used

| | Value |
| ----------------------------------- | -------------------------------------- |
Expand All @@ -23,11 +23,14 @@ ms.author: mavasani

## Cause

The <xref:System.Linq.Enumerable.Count%2A> or <xref:System.Linq.Enumerable.LongCount%2A> method was used where the <xref:System.Linq.Enumerable.Any%2A> method would be more efficient.
The [Count()](xref:System.Linq.Enumerable.Count%2A) or [LongCount()](xref:System.Linq.Enumerable.LongCount%2A) *method* was used where the [Any()](xref:System.Linq.Enumerable.Any%2A) method would be more efficient.

## Rule description

This rule flags the <xref:System.Linq.Enumerable.Count%2A> and <xref:System.Linq.Enumerable.LongCount%2A> LINQ method calls used to check if the collection has at least one element. These method calls require enumerating the entire collection to compute the count. The same check is faster with the <xref:System.Linq.Enumerable.Any%2A> method as it avoids enumerating the collection.
This rule flags [Count()](xref:System.Linq.Enumerable.Count%2A) and [LongCount()](xref:System.Linq.Enumerable.LongCount%2A) LINQ method calls that are used to check if the collection has at least one element. These methods enumerate the entire collection to compute the count. The same check is faster with the [Any()](xref:System.Linq.Enumerable.Any%2A) method as it avoids enumerating the collection.

> [!NOTE]
> This rule is similar to [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md). However that rule suggests using the `Count` *property*, while this rule applies to the Linq `Count()` *extension method*.

## How to fix violations

Expand Down Expand Up @@ -93,7 +96,8 @@ For more information, see [How to suppress code analysis warnings](../suppress-w

- [CA1826: Use property instead of Linq Enumerable method](ca1826.md)
- [CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md)
- [CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md)
- [CA1829: Use Length/Count property instead of Enumerable.Count() method](ca1829.md)
- [CA1860: Avoid using 'Enumerable.Any()' extension method](ca1860.md)

## See also

Expand Down
17 changes: 13 additions & 4 deletions docs/fundamentals/code-analysis/quality-rules/ca1860.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA1860: Avoid using 'Enumerable.Any()' extension method"
description: "Learn about code analyzer rule CA1860 - Avoid using 'Enumerable.Any()' extension method"
ms.date: 03/01/2023
ms.date: 07/17/2023
ms.topic: reference
f1_keywords:
- CA1860
Expand All @@ -24,17 +24,20 @@ dev_langs:

## Cause

<xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> is called on a type that has a `Length`, `Count`, or `IsEmpty` property.
<xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> is called on a type that has a `Length`, `Count`, or `IsEmpty` *property*.

## Rule description

It's more efficient and clearer to use `Length`, `Count`, or `IsEmpty` (if possible) than to call <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> to determine whether a collection type has any elements.
To determine whether a collection type has any elements, it's more efficient and clearer to use the `Length`, `Count`, or `IsEmpty` (if possible) properties than to call the <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> method.

`Any()`, which is an extension method, uses language integrated query (LINQ). It's more efficient to rely on the collection's own properties, and it also clarifies intent.

> [!NOTE]
> This rule is similar to [CA1827: Do not use Count()/LongCount() when Any() can be used](ca1827.md). However, that rule applies to the Linq `Count()` *method*, while this rule suggests using the `Count` *property*.

## How to fix violations

Replace a call to <xref:System.Linq.Enumerable.Any%2A?displayProperty=nameWithType> with a call to the collection's `Length`, `Count`, or `IsEmpty` property.
Replace a call to [Any()](xref:System.Linq.Enumerable.Any%2A) with a call to the collection's `Length`, `Count`, or `IsEmpty` property.

## Example

Expand Down Expand Up @@ -90,3 +93,9 @@ dotnet_diagnostic.CA1860.severity = none
```

For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

## Related rules

- [CA1827: Do not use Count/LongCount when Any can be used](ca1827.md)
- [CA1828: Do not use CountAsync/LongCountAsync when AnyAsync can be used](ca1828.md)
- [CA1829: Use Length/Count property instead of Enumerable.Count method](ca1829.md)