Linq 的Set 操作....
共4个操作.distinct、Union、Intersect、Except.
简单实例:
共4个操作.distinct、Union、Intersect、Except.
distinct
下图演示 Enumerable.Distinct 方法对字符序列的行为。返回的序列包含输入序列的唯一元素。
Except
下图演示 Enumerable.Except 的行为。返回的序列只包含位于第一个输入序列但不位于第二个输入序列的元素。
Intersect
下图演示 Enumerable.Intersect 的行为。返回的序列包含两个输入序列共有的元素。
Union
下图演示对两个字符序列执行的联合操作。返回的序列包含两个输入序列的唯一的元素。
简单实例:
- #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", "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", 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", "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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。