• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Enumerable.SelectMany方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Enumerable.SequenceEqual方法代码示例发布时间:2022-05-26
下一篇:
C# Enumerable.Select方法代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap