Linq
LINQ: 语言集成查询程序集(库) (language Integrated Query )
使用Linq里的方法,可以快速实现对IEnumerable对象(集合数据)的操作处理。有以下细节需要注意:
(一).使用时需要添加 命名空间:using System Linq;
程序集:System.Core.dll 提供一组用于查询实现 IEnumerable 的对象的 static()方法。
(二).在此程序集里包含一个***静态static的类***:IEnumerable类,这个类里包含***很多静态static的方法***。
(三). Enumerable 类:public static class Enumerable。
常用的 Enumerable 类的static方法主要有:
1.Where():添加链接描述
2.Select ():添加链接描述
3.Contains():
4.Take(n):从序列的开头开始,返回n个连续元素。
5.Skip(n):跳过指定的数量的序列中的元素,然后返回剩余元素。
6.OrderBy /OrderByDescending
7.Count
8.ToArray() : Arr2=Arr1.ToArray()方法将所有列表中的元素复制一份给到新的数组Arr2,或者说该函数将列表转换为数组。
9.Reverse
Take() 、Skip() 、ToArray()示例:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
namespace ListTest
{
class Program
{
static void printlist(List<int> lst)
{
foreach (var item in lst)
{
Console.Write(item+" ");
}
Console.Write("lst.Count:"+lst.Count+"\n");
}
static void Main(string[] args)
{
List<int> a = new List<int>();
a.Add(20);
a.Add(10);
a.Add(30);
a.Add(40);
a.Add(50);
Console.WriteLine("list elements...");
printlist(a);
Console.WriteLine("type of a: " + a.GetType());
int[] arr = a.Take(3).Skip(1).ToArray();
Console.WriteLine("Hello World!");
Console.WriteLine("type of arr: " + arr.GetType());
foreach (var itemn in arr)
{
Console.Write(itemn + " ,");
}
Console.WriteLine(arr.Count());
Console.ReadKey();
}
}
}
output:
list elements…
20 10 30 40 50 lst.Count:5
type of a: System.Collections.Generic.List`1[System.Int32]
Hello World!
type of arr: System.Int32[]
10 ,30 ,2
**
var
**
var说明