个性化阅读
专注于IT技术分析

LINQ转字符串数组

本文概述

LINQ to String Array表示在字符串数组上编写LINQ查询以获取所需的数据。如果我们在字符串数组上使用LINQ查询, 则无需编写太多代码即可轻松获得所需的元素。

LINQ到字符串数组的语法

这是在字符串Array上编写LINQ查询以从数组集合中获取所需元素的语法。

IEnumerable<string> result = from a in arr

                                         select a;

在以上语法中, 我们编写了LINQ查询以从” arr”字符串数组获取数据。

LINQ to String Array的示例

这是LINQ to String Array的示例, 用于从名称以” S”开头的字符串数组序列中获取元素。

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

namespace ConsoleApp1
{
    class Programme2
    {
        static void Main(string[] args)
        {
//create an array of type string
            string[] array = { "Vaishali", "Shalu", "Akshay", "Akki" };
/*IEnumerable will iterate over the collection of data use 
Linq query to select the particular element which starts from s*/
            IEnumerable<string> result = from a in array
            where a.ToLowerInvariant().StartsWith("s")
            select a;
    //foreach loop is used to print the output which is in the result
            foreach (string item in result)
            {
                Console.WriteLine(item);
            }
                Console.ReadLine();
         }
    }
}

输出

LINQ转字符串数组

LINQ转Int数组

赞(0)
未经允许不得转载:srcmini » LINQ转字符串数组

评论 抢沙发

评论前必须登录!