原文地址:https://msdn.microsoft.com/zh-cn/library/bb546168.aspx#Mtps_DropDownFilterText

 
其他版本
 

还可以在不更改原始对象的情况下映射该对象。

下面一节中列出了执行投影的标准查询运算符方法。

方法
 
 

方法名

说明

C# 查询表达式语法

Visual Basic 查询表达式语法

更多信息

Select

映射基于转换函数的值。

select

Select

Enumerable.Select

Queryable.Select

SelectMany

映射基于转换函数的值序列,然后将它们展平为一个序列。

from 子句

From 子句

Enumerable.SelectMany

Queryable.SelectMany

Select

Select 子句来映射字符串列表中每个字符串的第一个字母。

 
            List<string> words = new List<string>() { "an", "apple", "a", "day" };

            var query = from word in words
                        select word.Substring(0, 1);

            foreach (string s in query)
                Console.WriteLine(s);

            /* This code produces the following output:

                a
                a
                a
                d
            */



SelectMany

From 子句(在 Visual Basic 中)来映射字符串列表中每个字符串中的每个单词。

 
            List<string> phrases = new List<string>() { "an apple a day", "the quick brown fox" };

            var query = from phrase in phrases
                        from word in phrase.Split(' ')
                        select word;

            foreach (string s in query)
                Console.WriteLine(s);

            /* This code produces the following output:

                an
                apple
                a
                day
                the
                quick
                brown
                fox
            */



然后,SelectMany() 将串联这些可枚举序列以创建一个大的序列。

在每种情况下,假定选择器(转换)函数从每个源值中选择一个由花卉数据组成的数组。

下图描述 Select() 如何返回一个与源集合具有相同元素数目的集合。

下图描述 SelectMany() 如何将中间数组序列串联为一个最终结果值,其中包含每个中间数组中的每个值。 代码示例

For Each)循环,以便枚举每个子序列中的每个字符串。

 
class Bouquet
{
    public List<string> Flowers { get; set; }
}

static void SelectVsSelectMany()
{
    List<Bouquet> bouquets = new List<Bouquet>() {
        new Bouquet { Flowers = new List<string> { "sunflower", "daisy", "daffodil", "larkspur" }},
        new Bouquet{ Flowers = new List<string> { "tulip", "rose", "orchid" }},
        new Bouquet{ Flowers = new List<string> { "gladiolis", "lily", "snapdragon", "aster", "protea" }},
        new Bouquet{ Flowers = new List<string> { "larkspur", "lilac", "iris", "dahlia" }}
    };

    // *********** Select ***********            
    IEnumerable<List<string>> query1 = bouquets.Select(bq => bq.Flowers);

    // ********* SelectMany *********
    IEnumerable<string> query2 = bouquets.SelectMany(bq => bq.Flowers);

    Console.WriteLine("Results by using Select():");
    // Note the extra foreach loop here.
    foreach (IEnumerable<String> collection in query1)
        foreach (string item in collection)
            Console.WriteLine(item);

    Console.WriteLine("\nResults by using SelectMany():");
    foreach (string item in query2)
        Console.WriteLine(item);

    /* This code produces the following output:

       Results by using Select():
        sunflower
        daisy
        daffodil
        larkspur
        tulip
        rose
        orchid
        gladiolis
        lily
        snapdragon
        aster
        protea
        larkspur
        lilac
        iris
        dahlia

       Results by using SelectMany():
        sunflower
        daisy
        daffodil
        larkspur
        tulip
        rose
        orchid
        gladiolis
        lily
        snapdragon
        aster
        protea
        larkspur
        lilac
        iris
        dahlia
    */

}

相关文章:

  • 2022-02-10
  • 2022-01-23
  • 2022-01-14
  • 2021-06-25
  • 2022-12-23
猜你喜欢
  • 2021-10-27
  • 2021-07-09
  • 2022-01-19
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-01-19
相关资源
相似解决方案