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

C# Queryable.SelectMany方法代码示例

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

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



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

示例1: SelectManyEx3

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

class Pet
{
    public string Name { get; set; }
    public string Breed { get; set; }
}

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

    // This query demonstrates how to obtain a sequence of
    // the names of all the pets whose breed is "Collie", while
    // keeping an association with the owner that owns the pet.
    var query =
        petOwners.AsQueryable()
        // Create a sequence of ALL the Pet objects. Then
        // project an anonymous type that consists of each
        // Pet in the new sequence and the PetOwner object
        // from the initial array that corresponds to that pet.
       .SelectMany(owner => owner.Pets,
                   (owner, pet) => new { owner, pet })
        // Filter the sequence of anonymous types to only
        // keep pets whose breed is "Collie".
        .Where(ownerAndPet => ownerAndPet.pet.Breed == "Collie")
        // Project an anonymous type that consists
        // of the pet owner's name and the pet's name.
        .Select(ownerAndPet => new
        {
            Owner = ownerAndPet.owner.Name,
            Pet = ownerAndPet.pet.Name
        });

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

输出:

{ Owner = Ashkenazi, Pet = Walker }
    { Owner = Price, Pet = Diesel }
    { Owner = Hines, Pet = Dusty }


示例2: 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" } } };

    // For each PetOwner element in the source array,
    // project a sequence of strings where each string
    // consists of the index of the PetOwner element in the
    // source array and the name of each pet in PetOwner.Pets.
    IEnumerable<string> query =
        petOwners.AsQueryable()
        .SelectMany(
        (petOwner, index) => petOwner.Pets.Select(pet => index + pet)
        );

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

输出:

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


示例3: 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.AsQueryable().SelectMany(petOwner => petOwner.Pets);

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

    // Only one foreach loop is required to iterate through the
    // results because 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.AsQueryable().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,代码行数:44,代码来源:Queryable.SelectMany

输出:

Using SelectMany():
    Scruffy
    Sam
    Walker
    Sugar
    Scratches
    Diesel

    Using Select():
    Scruffy
    Sam

    Walker
    Sugar

    Scratches
    Diesel


示例4: Main

//引入命名空间
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {
        string[] presidents = {"ant", "arding", "rison", "eyes", "over", "ackson"};
        IEnumerable<string> items = new[] {
                                   presidents.Take(5),
                                   presidents.Skip(5)
                                  }.SelectMany(s => s);
        foreach (string item in items)
            Console.WriteLine(item);
    }
}
开发者ID:C#程序员,项目名称:System.Linq,代码行数:16,代码来源:Queryable.SelectMany



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C# Queryable.SequenceEqual方法代码示例发布时间:2022-05-26
下一篇:
C# Queryable.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