Enumerable.Any 方法

定義

判斷序列的任何項目是否符合條件。

多載

Any<TSource>(IEnumerable<TSource>)

判斷序列是否包含任何項目。

Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

判斷序列的任何項目是否符合條件。

Any<TSource>(IEnumerable<TSource>)

來源:
AnyAll.cs
來源:
AnyAll.cs
來源:
AnyAll.cs

判斷序列是否包含任何項目。

C#
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

要檢查是否為空白的 IEnumerable<T>

傳回

如果來源序列包含任何項目,則為 true,否則為 false

例外狀況

sourcenull

範例

下列程式代碼範例示範如何使用 Any 來判斷序列是否包含任何專案。

C#
List<int> numbers = new List<int> { 1, 2 };
bool hasElements = numbers.Any();

Console.WriteLine("The list {0} empty.",
    hasElements ? "is not" : "is");

// This code produces the following output:
//
// The list is not empty.

方法傳回的布爾值Any<TSource>(IEnumerable<TSource>)通常用於 Visual Basic 中子句的述詞 (子句) Where 或直接呼叫 Where<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)where 方法。 下列範例示範這個 方法的使用 Any 方式。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}
class Person
{
    public string LastName { get; set; }
    public Pet[] Pets { get; set; }
}

public static void AnyEx2()
{
    List<Person> people = new List<Person>
        { new Person { LastName = "Haas",
                       Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
                                          new Pet { Name="Boots", Age=14 },
                                          new Pet { Name="Whiskers", Age=6 }}},
          new Person { LastName = "Fakhouri",
                       Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
          new Person { LastName = "Antebi",
                       Pets = new Pet[] { }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have a non-empty Pet array.
    IEnumerable<string> names = from person in people
                                where person.Pets.Any()
                                select person.LastName;

    foreach (string name in names)
    {
        Console.WriteLine(name);
    }

    /* This code produces the following output:

       Haas
       Fakhouri
       Philips
    */
}

備註

注意

這個方法不會傳回集合的任何一個專案。 相反地,它會判斷集合是否包含任何專案。

只要判斷結果,就會停止 列舉 source

在 Visual Basic 查詢表達式語法中, Aggregate Into Any() 子句會轉譯為的 Any調用。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Any<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

來源:
AnyAll.cs
來源:
AnyAll.cs
來源:
AnyAll.cs

判斷序列的任何項目是否符合條件。

C#
public static bool Any<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

其項目要套用述詞的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用來測試每個項目是否符合條件的函式。

傳回

若來源序列不是空的且其至少有一個元素通過指定述詞中的測試,則為 true;否則為 false

例外狀況

sourcepredicatenull

範例

下列程式代碼範例示範如何使用 Any 來判斷序列中的任何專案是否符合條件。

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Vaccinated { get; set; }
}

public static void AnyEx3()
{
    // Create an array of Pets.
    Pet[] pets =
        { new Pet { Name="Barley", Age=8, Vaccinated=true },
          new Pet { Name="Boots", Age=4, Vaccinated=false },
          new Pet { Name="Whiskers", Age=1, Vaccinated=false } };

    // Determine whether any pets over age 1 are also unvaccinated.
    bool unvaccinated =
        pets.Any(p => p.Age > 1 && p.Vaccinated == false);

    Console.WriteLine(
        "There {0} unvaccinated animals over age one.",
        unvaccinated ? "are" : "are not any");
}

// This code produces the following output:
//
//  There are unvaccinated animals over age one.

備註

注意

這個方法不會傳回集合的任何一個專案。 相反地,它會判斷集合的任何專案是否符合條件。

只要判斷結果,就會停止 列舉 source

在 Visual Basic 查詢表達式語法中, Aggregate Into Any() 子句會轉譯為的 Any調用。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0