LINQ to String Array意味着在字符串数组上编写LINQ查询以获取所需的数据。如果无涯教程在字符串数组上使用LINQ查询,可以轻松地获得所需的元素,而无需编写大量代码。
以下是编写字符串数组上的LINQ查询以从数组集合中获取所需元素的语法。
链接:https://www.learnfk.comhttps://www.learnfk.com/linq/linq-to-string-array.html
来源:LearnFk无涯教程网
IEnumerableresult = from a in arr select a;
在上面的语法中,我们编写了 LINQ 查询来从“arr”字符串数组中获取数据。
这是 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) { //创建一个字符串类型的数组 string[] array = { "Vaishali", "Shalu", "Akshay", "Akki" }; /*IEnumerable 将遍历收集的数据使用 Linq 查询以选择从 s 开始的特定元素*/ IEnumerableresult = from a in array where a.ToLowerInvariant().StartsWith("s") select a; //foreach 循环用于打印结果中的输出 foreach (string item in result) { Console.WriteLine(item); } Console.ReadLine(); } } }
输出:
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)