微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Linq的Set操作

 Linq 的Set 操作....
共4个操作.distinct、Union、Intersect、Except.

distinct

下图演示 Enumerable.Distinct 方法对字符序列的行为。返回的序列包含输入序列的唯一元素。

显示 Distinct() 的行为的图。

Except

下图演示 Enumerable.Except 的行为。返回的序列只包含位于第一个输入序列但不位于第二个输入序列的元素。

显示 Except() 的操作的图。

Intersect

下图演示 Enumerable.Intersect 的行为。返回的序列包含两个输入序列共有的元素。

显示两个序列的交叉的图。

Union

下图演示对两个字符序列执行的联合操作。返回的序列包含两个输入序列的唯一的元素。

显示两个序列的联合的图。


简单实例:
  1.  #region Set Operator
  2.         /* The distinct原型
  3.          * 
  4.          * 功能:去掉重复的元素
  5.          * 与sqlServer中distinct关键字一样
  6.          * 
  7.          *   public static IEnumerable<T> distinct<T>(
  8.          *   this IEnumerable<T> source);
  9.          *  
  10.          */
  11.         static void distinctOperatorPrototype()
  12.         {
  13.             string[] presidents = { "Adams""Arthur""Buchanan""Bush""Carter""Cleveland" };
  14.             Console.WriteLine("presidents Count:"+presidents.Count());
  15.             IEnumerable<string> doublePresidents = presidents.Concat(presidents);
  16.             Console.WriteLine(" after Concat presidents Count:" + doublePresidents.Count());
  17.             IEnumerable<stringdistinctPresidents = doublePresidents.distinct();
  18.             Console.WriteLine("after distinct presidents Count:" + distinctPresidents.Count());
  19.         }
  20.         /*The Union原型
  21.          * 
  22.          * 功能:将多个集合运算成一个集合,消除重复项
  23.          * 和sqlServer中关键字Union关键字一样.
  24.          * 
  25.          * public static IEnumerable<T> Union<T>(
  26.          *    this IEnumerable<T> first,
  27.          *    IEnumerable<T> second);
  28.          *  
  29.          */
  30.         static void UnionPrototype()
  31.         {
  32.             string[] presidents = { "Adams""Cleveland" };
  33.             Console.WriteLine("presidents element count:{0}", presidents.Count());
  34.             IEnumerable<string> first = presidents.Take(4);//取前4个元素
  35.             IEnumerable<string> second = presidents.Skip(2);//去掉前2个元素
  36.             Console.WriteLine("first elements count:{0}", first.Count());
  37.             Console.WriteLine("second elements count:{0}", second.Count());
  38.             IEnumerable<string> concat = first.Concat(second);
  39.             IEnumerable<string> union = first.Union(second);
  40.          
  41.             Console.WriteLine("after concat element count:{0}", concat.Count());
  42.             Console.WriteLine("after union element count:{0}", union.Count());
  43.         }
  44.         /*Intersect 原型
  45.          * 
  46.          * 功能:取2个集合的交集
  47.          * 
  48.          * public static IEnumerable<T> Intersect<T>(
  49.          *   this IEnumerable<T> first,
  50.          *   IEnumerable<T> second);
  51.          *
  52.          */
  53.         static void IntersectPrototype()
  54.         {
  55.             string[] presidents = { "Adams", second.Count());
  56.             IEnumerable<string> concat = first.Concat(second);
  57.             IEnumerable<string> intersect = first.Intersect(second);
  58.             Console.WriteLine("after concat element count:{0}", concat.Count());
  59.             Console.WriteLine("after intersect element count:{0}", intersect.Count());
  60.         }
  61.         /*Except原型
  62.          * 
  63.          * 功能返回的序列只包含位于第一个输入序列但不位于第二个输入序列的元素
  64.          * 
  65.          * public static IEnumerable<T> Except<T>(
  66.          *   this IEnumerable<T> first,
  67.          *   IEnumerable<T> second);
  68.          * 
  69.          * 
  70.          * 
  71.          */
  72.         static void ExceptPrototype()
  73.         {
  74.             string[] presidents = { "Adams""Cleveland" };
  75.             Console.WriteLine("presidents element count:{0}", presidents.Count());
  76.             IEnumerable<string> first = presidents.Take(4);//取前4个元素
  77.             Console.WriteLine("first elements count:{0}", first.Count());
  78.             IEnumerable<string> except = presidents.Except(first);
  79.             Console.WriteLine("after except element count:{0}", except.Count());
  80.             foreach (string item in except)
  81.             {
  82.                 Console.WriteLine(item);
  83.             }
  84.         }
  85.         #endregion

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐