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


C# Enumerable.SelectMany方法代码示例

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


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

示例1: SelectManyEx3

class PetOwner
{
    public string Name { get; set; }
    public List<string> Pets { get; set; }
}

public static void SelectManyEx3()
{
    PetOwner[] petOwners =
        { new PetOwner { Name="Higa", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price", 
              Pets = new List<string>{ "Scratches", "Diesel" } },
          new PetOwner { Name="Hines", 
              Pets = new List<string>{ "Dusty" } } };

    // Project the pet owner's name and the pet's name.
    var query =
        petOwners
        .SelectMany(petOwner => petOwner.Pets, (petOwner, petName) => new { petOwner, petName })
        .Where(ownerAndPet => ownerAndPet.petName.StartsWith("S"))
        .Select(ownerAndPet =>
                new
                {
                    Owner = ownerAndPet.petOwner.Name,
                    Pet = ownerAndPet.petName
                }
        );

    // Print the results.
    foreach (var obj in query)
    {
        Console.WriteLine(obj);
    }
}
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:37,代码来源:Enumerable.SelectMany

输出:

{Owner=Higa, Pet=Scruffy}
{Owner=Higa, Pet=Sam}
{Owner=Ashkenazi, Pet=Sugar}
{Owner=Price, Pet=Scratches}

示例2: SelectManyEx1

class PetOwner
           {
               public string Name { get; set; }
               public List<String> Pets { get; set; }
           }

           public static void SelectManyEx1()
           {
               PetOwner[] petOwners = 
                   { new PetOwner { Name="Higa, Sidney", 
                         Pets = new List<string>{ "Scruffy", "Sam" } },
                     new PetOwner { Name="Ashkenazi, Ronen", 
                         Pets = new List<string>{ "Walker", "Sugar" } },
                     new PetOwner { Name="Price, Vernette", 
                         Pets = new List<string>{ "Scratches", "Diesel" } } };

               // Query using SelectMany().
               IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);

               Console.WriteLine("Using SelectMany():");

               // Only one foreach loop is required to iterate 
               // through the results since it is a
               // one-dimensional collection.
               foreach (string pet in query1)
               {
                   Console.WriteLine(pet);
               }

               // This code shows how to use Select() 
               // instead of SelectMany().
               IEnumerable<List<String>> query2 =
                   petOwners.Select(petOwner => petOwner.Pets);

               Console.WriteLine("\nUsing Select():");

               // Notice that two foreach loops are required to 
               // iterate through the results
               // because the query returns a collection of arrays.
               foreach (List<String> petList in query2)
               {
                   foreach (string pet in petList)
                   {
                       Console.WriteLine(pet);
                   }
                   Console.WriteLine();
               }
           }
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:48,代码来源:Enumerable.SelectMany

输出:

Using SelectMany():
            Scruffy
            Sam
            Walker
            Sugar
            Scratches
            Diesel

            Using Select():
            Scruffy
            Sam

            Walker
            Sugar

            Scratches
            Diesel

示例3: SelectManyEx2

class PetOwner
{
    public string Name { get; set; }
    public List<string> Pets { get; set; }
}

public static void SelectManyEx2()
{
    PetOwner[] petOwners = 
        { new PetOwner { Name="Higa, Sidney", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi, Ronen", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price, Vernette", 
              Pets = new List<string>{ "Scratches", "Diesel" } },
          new PetOwner { Name="Hines, Patrick", 
              Pets = new List<string>{ "Dusty" } } };

    // Project the items in the array by appending the index 
    // of each PetOwner to each pet's name in that petOwner's 
    // array of pets.
    IEnumerable<string> query =
        petOwners.SelectMany((petOwner, index) =>
                                 petOwner.Pets.Select(pet => index + pet));

    foreach (string pet in query)
    {
        Console.WriteLine(pet);
    }
}
开发者ID:.NET开发者,项目名称:System.Linq,代码行数:30,代码来源:Enumerable.SelectMany

输出:

0Scruffy
0Sam
1Walker
1Sugar
2Scratches
2Diesel
3Dusty


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