LINQ ToList()方法

本文概述

在LINQ中, ToList运算符从给定的源中获取元素, 然后返回一个新的List。因此, 在这种情况下, 输入将被转换为List类型。

LINQ ToList()运算符的语法

使用LINQ ToList()将输入集合转换为列表的语法。

C#代码

List<string> result = countries.ToList();

在以上语法中, 我们使用LINQ ToList()运算符将”国家”集合转换为列表。

方法语法中的ToList()运算符示例

使用LINQ ToList()将输入集合转换为方法语法的示例。

C#代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
//create array countries of type string containing the collection of data
            string[] countries = { "US", "UK", "India", "Russia", "China", "Australia", "Argentina" };
//countries.ToList() convert the collection of data into the list.
            List<string> result = countries.ToList();
    //foreach loop is used to print the information of the student
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }
                Console.ReadLine();
        }
    }
}

输出

LINQ ToList()方法

在上面的示例中, 我们使用LINQ ToList()方法将国家/地区集合转换为列表。

查询语法中的ToList()运算符示例

在查询语法中使用LINQ ToList()运算符的示例

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] countries = { "US", "UK", "India", "Russia", "China", "Australia", "Argentina" };
//used query syntax to convert the collection of the data into the list
            List<string> result = (from x in countries select x).ToList();
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }
                Console.ReadLine();
        }
    }
}

输出

LINQ ToList()方法

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_48587.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?