1.用Where()方法进行筛选
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 89, 10 };
//查询所有能被2整除的元素
var query1 = intary.Where(num => num % 2 == 0);
Console.WriteLine("Query1:");
foreach (var item in query1)
{
Console.WriteLine("{0}", item);
}
//查询所有值大于3被索引的元素
var query2 = intary.Where((num, index) => num > index * 3);
Console.WriteLine("Query2:");
foreach (var item in query2)
{
Console.WriteLine("{0}", item);
}
}
}
}
2.用OrderBy()方法进行排序
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 89, 10 };
//对所有元素按照%10进行升序排列,这是默认的排序方式
var query1 = intary.OrderBy(val => val % 10 );
Console.WriteLine("Query1:");
foreach (var item in query1)
{
Console.WriteLine("{0}", item);
}
//对所有元素按照%10进行降序排列
var query2 = intary.OrderByDescending(val => val % 10);
Console.WriteLine("Query2:");
foreach (var item in query2)
{
Console.WriteLine("{0}", item);
}
}
}
}
以上的两个排序Demo都是采用默认的int排序比较器,在一些开发中,以下两种情况需要使用特定的数据比较器:
using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApplication3
{
//自定义int类型比较器,实现IComparable<int>接口
class MyCompare : IComparer<int>
{
public int Compare(int x, int y)
{
int x1 = Math.Abs(x);
int y1 = Math.Abs(y);
if (x1 > y1)
{
return 1;
}
else if (x1 == y1)
{
return 0;
}
else
{
return -1;
}
}
}
class Program
{
static void Main(string[] args)
{
//实例化自定义比较器
MyCompare mc = new MyCompare();
int[] intary = { 1, -2, 3, -4, 5, -6, 7, -8, 9, -10 };
var query1 = intary.OrderBy(val => val, mc);
Console.WriteLine("Query1");
foreach (var item in query1)
{
Console.WriteLine("{0}",item);
}
}
}
}
3.用Skip()、SkipWhile()跳过元素
using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
//跳过intAry中前3个元素
var query1 = intary.Skip(3);
Console.WriteLine("Query1");
foreach (var item in query1)
{
Console.WriteLine("{0}", item);
}
//跳过intAry中小于5的元素
var query2 = intary.SkipWhile(num => num < 5);
Console.WriteLine("Query2");
foreach (var item in query2)
{
Console.WriteLine("{0}", item);
}
}
}
}
4.用Take()、TakeWhile()提取元素
using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
//取intAry中前3个元素
var query1 = intary.Take(3);
Console.WriteLine("Query1");
foreach (var item in query1)
{
Console.WriteLine("{0}", item);
}
//取intAry中小于5的元素
var query2 = intary.TakeWhile(num => num < 5);
Console.WriteLine("Query2");
foreach (var item in query2)
{
Console.WriteLine("{0}", item);
}
}
}
}
5.对元素进行数值计算
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40 };
var intMin = intary.Min();
var intMax = intary.Max();
var intSum = intary.Sum();
var intAverage = intary.Average();
Console.WriteLine("intary's min={0},max={1},sum={2},average={3}",intMin,intMax,intSum,intAverage);
string[] strAry = { "Kevin", "James", "Ken", "Ben" };
var strMin = strAry.Min();
var strMax = strAry.Max();
Console.WriteLine("strAry's Min = {0},Max={1}",strMin,strMax);
}
}
}
6.用Distinct()消除集合中相等的元素
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intary = { 1,1, 2,2, 3, 3, 4, 5};
var query1 = intary.Distinct();
foreach (var item in query1)
{
Console.WriteLine(item);
}
}
}
}
7.用Concat()连接两个集合
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string[] strAry1 = {"Kevin","James","Ben"};
string[] strAry2 = { "Ken","Joanna","cc"};
var query1 = strAry1.Concat(strAry2);
Console.WriteLine("Query1");
foreach (var item in query1)
{
Console.WriteLine(item);
}
var query2 = strAry2.Concat(strAry1);
Console.WriteLine("Query2");
foreach (var item in query2)
{
Console.WriteLine(item);
}
}
}
}
注意:Concat()方法是直接将两个集合中的元素连接在一起,不会进行重新排序、过滤等,就算两个集合中元素有重复也同样保留。
8.集合操作
说到集合,很容易想起并集、交集、差集3种常用操作。在LINQ中,IEnumerable<T>类分别通过Union()、Intersect()、Except完成这3种操作()。
using System;
using System.Linq;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int[] intAry1 = {1,2,3};
int[] intAry2 = {3,4,5};
var query1 = intAry1.Union(intAry2);
Console.WriteLine("并集");
foreach (var item in query1)
{
Console.WriteLine(item);
}
var query2 = intAry1.Intersect(intAry2);
Console.WriteLine("交集");
foreach (var item in query2)
{
Console.WriteLine(item);
}
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛