当前位置: 首页>>代码示例>>C#>>正文


C# Queryable.All<TSource>方法代码示例

本文整理汇总了C#中System.Linq.Queryable.All<TSource>方法的典型用法代码示例。如果您正苦于以下问题:C# Queryable.All<TSource>方法的具体用法?C# Queryable.All<TSource>怎么用?C# Queryable.All<TSource>使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了Queryable.All<TSource>方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AllEx1

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void AllEx1()
{
    // Create an array of Pets.
    Pet[] pets = { new Pet { Name="Barley", Age=10 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=6 } };

    // Determine whether all pet names in the array start with 'B'.
    bool allStartWithB =
        pets.AsQueryable().All(pet => pet.Name.StartsWith("B"));

    Console.WriteLine(
        "{0} pet names start with 'B'.",
        allStartWithB ? "All" : "Not all");
}
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:21,代码来源:Queryable.All

输出:

Not all pet names start with 'B'.

示例2: AllEx2

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 AllEx2()
{
    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 Pet { Name = "Belle", Age = 8} }},
          new Person { LastName = "Philips",
                       Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
                                          new Pet { Name = "Rover", Age = 13}} }
        };

    // Determine which people have pets that are all older than 5.
    IEnumerable<string> names = from person in people
                                where person.Pets.AsQueryable().All(pet => pet.Age > 5)
                                select person.LastName;

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

    /* This code produces the following output:
     * 
     * Haas
     * Antebi
     */
}
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:41,代码来源:Queryable.All


注:本文中的System.Linq.Queryable.All<TSource>方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。