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


C# Queryable.SelectMany方法代码示例

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