Iesi.Collections.dll的功能介绍

来源:互联网 发布:电脑美图软件 编辑:程序博客网 时间:2024/06/15 19:21

由于.NET框架本身没有提供集合运算功能,在使用这方面的功能时,我们可以借助第三方的类库来实现( 现在有了Linq,那就方便多了)。在NHibernate 框架中有个Iesi.Collection.dll,这个类库提供了集合运算功能,并且支持泛型。

功能: 主要是取得2个集合里,相同、相异、联集的部份。
例子:
[csharp] view plaincopy
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Web.UI.WebControls.WebParts;  
  9. using System.Web.UI.HtmlControls;  
  10. using Iesi.Collections.Generic;  
  11.   
  12. public partial class _Default : System.Web.UI.Page   
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         ISet<string> Girls = new HashedSet<string>();     
  17.          Girls.Add("Christine");     
  18.          Girls.Add("Eva");     
  19.          Girls.Add("Jean");     
  20.          Girls.Add("Novia");     
  21.         Girls.Add("Winnie");     
  22.      
  23.          ISet<string> PMs = new HashedSet<string>();     
  24.          PMs.Add("Eva");     
  25.          PMs.Add("Novia");     
  26.          PMs.Add("Vincent");     
  27.          PMs.Add("Williams");     
  28.          PMs.Add("Winnie");     
  29.      
  30.          ISet<string> GirlPMs = Girls.Intersect(PMs);             
  31.          Response.Write("是女生且是PM: <br />");     
  32.          foreach (string s in GirlPMs) {     
  33.              Response.Write(s + "<br />");     
  34.          }     
  35.      
  36.          Response.Write("<br />");     
  37.          ISet<string> GirlNotPMs = Girls.Minus(PMs);     
  38.          Response.Write("是女生且不是PM: <br />");     
  39.          foreach (string s in GirlNotPMs) {     
  40.              Response.Write(s + "<br />");     
  41.          }     
  42.      
  43.          Response.Write("<br />");     
  44.          ISet<string> GirlOrPMs = Girls.Union(PMs);     
  45.          Response.Write("是女生或是PM: <br />");     
  46.          foreach (string s in GirlOrPMs) {     
  47.              Response.Write(s + "<br />");     
  48.          }     
  49.      
  50.          Response.Write("<br />");     
  51.          ISet<string> NotMatch = Girls.ExclusiveOr(PMs);     
  52.          Response.Write("是女生但不是PM,或是PM但不是女生: <br />");     
  53.          foreach (string s in NotMatch) {     
  54.              Response.Write(s + "<br />");     
  55.          }     
  56.   
  57.     }  
  58. }  


 

运行结果: 

是女生且是PM: 
Eva
Novia
Winnie

是女生且不是PM: 
Christine
Jean

是女生或是PM: 
Christine
Eva
Jean
Novia
Winnie
Vincent
Williams

是女生但不是PM,或是PM但不是女生: 
Christine
Williams
Jean
Vincent

补充:
1. 如果顺序是重要的,那 HashedSet 可以改成 SortedSet
2. 如果用SortedSet, 集合里面的元素必需继承IComparable接口
3. 更详细的介绍可参考 Add Support for "Set" Collections to .NET
0 0
原创粉丝点击