1、启动Visual Studio 2010,新建一个控制台应用程序,项目名称为“firstLINQ“。
2、在Program.cs中的Main方法中添加如下代码。
static void Main(string[] args) //使用LINQ查询进行数组的检索
{
//声明一个有6个元素的字符串数组names
string[] names={"Everett", "Albert", "George", "Harris", "David" };
var items=from name in names //声明一个匿名变量items,使用LINQ在数组names中找出
where name.Length>=6 //长度超过5的元素,将找到的字符串转为大写并排序,
orderby name //将LINQ执行的结果赋给匿名变量items;
select name.ToUpper(); //筛选出的name转换为大写字母
foreach(var item in items)
Console.WriteLine(item); //遍历items输出LINQ查询的结果
}
使用LINQ查询数组元素
本文介绍如何在Visual Studio 2010中创建名为'firstLINQ'的控制台应用,并利用LINQ来筛选数组中长度大于5的字符串元素,将其转换为大写并按字母顺序排列。
147

被折叠的 条评论
为什么被折叠?



