Linq 的Set 操作....
共4个操作.Distinct、Union、Intersect、Except.
#region Set Operator
/* The Distinct原型
*
* 功能:去掉重复的元素
* 与SqlServer中Distinct关键字一样
*
* public static IEnumerable<T> Distinct<T>(
* this IEnumerable<T> source);
*
*/
static void DistinctOperatorPrototype()
{
string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland" };
Console.WriteLine("presidents Count:"+presidents.Count());
IEnumerable<string> doublePresidents = presidents.Concat(presidents);
Console.WriteLine(" after Concat presidents Count:" + doublePresidents.Count());
IEnumerable<string> DistinctPresidents = doublePresidents.Distinct();
Console.WriteLine("after Distinct presidents Count:" + DistinctPresidents.Count());
}
/*The Union原型
*
* 功能:将多个集合运算成一个集合,消除重复项
* 和SqlServer中关键字Union关键字一样.
*
* public static IEnumerable<T> Union<T>(
* this IEnumerable<T> first,
* IEnumerable<T> second);
*
*/
static void UnionPrototype()
{
string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland" };
Console.WriteLine("presidents element count:{0}", presidents.Count());
IEnumerable<string> first = presidents.Take(4);//取前4个元素
IEnumerable<string> second = presidents.Skip(2);//去掉前2个元素
Console.WriteLine("first elements count:{0}", first.Count());
Console.WriteLine("second elements count:{0}", second.Count());
IEnumerable<string> concat = first.Concat(second);
IEnumerable<string> union = first.Union(second);
Console.WriteLine("after concat element count:{0}", concat.Count());
Console.WriteLine("after union element count:{0}", union.Count());
}
/*Intersect 原型
*
* 功能:取2个集合的交集
*
* public static IEnumerable<T> Intersect<T>(
* this IEnumerable<T> first,
* IEnumerable<T> second);
*
*/
static void IntersectPrototype()
{
string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland" };
Console.WriteLine("presidents element count:{0}", presidents.Count());
IEnumerable<string> first = presidents.Take(4);//取前4个元素
IEnumerable<string> second = presidents.Skip(2);//去掉前2个元素
Console.WriteLine("first elements count:{0}", first.Count());
Console.WriteLine("second elements count:{0}", second.Count());
IEnumerable<string> concat = first.Concat(second);
IEnumerable<string> intersect = first.Intersect(second);
Console.WriteLine("after concat element count:{0}", concat.Count());
Console.WriteLine("after intersect element count:{0}", intersect.Count());
}
/*Except原型
*
* 功能:返回的序列只包含位于第一个输入序列但不位于第二个输入序列的元素
*
* public static IEnumerable<T> Except<T>(
* this IEnumerable<T> first,
* IEnumerable<T> second);
*
*
*
*/
static void ExceptPrototype()
{
string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland" };
Console.WriteLine("presidents element count:{0}", presidents.Count());
IEnumerable<string> first = presidents.Take(4);//取前4个元素
Console.WriteLine("first elements count:{0}", first.Count());
IEnumerable<string> except = presidents.Except(first);
Console.WriteLine("after except element count:{0}", except.Count());
foreach (string item in except)
{
Console.WriteLine(item);
}
}
#endregion