一个数据筛选类

介绍了一种基于System.Collections.Comparer类实现的基本数据比较与集合筛选方法,通过多种示例演示了如何在不同场景下筛选数据。
Author: 水如烟  

参考应用类代码辅助类代码

利用System.Collections.Comparer类,可以做出比较基本数据大小的类,进而做出集合数据的筛选类。

所谓基本数据,特指除Object外的系统数据,即system.TypeCode枚举的类型。

一个类实例,是否属基本数据,可以这样判断:

         Public   Shared   Function  IsBaseType( ByVal  value  As   Object As   Boolean
            
If  value  Is   Nothing   Then   Return   False

            
Dim  mTypeCode  As   Integer   =  Type.GetTypeCode(value.GetType)

            
If  mTypeCode  =   1   Then   Return   False   ' Object

            
Dim  mTypeCodes  As   Integer ()  =   CType ([ Enum ].GetValues( GetType (TypeCode)),  Integer ())

            
Return   Not  (Array.IndexOf( Of   Integer )(mTypeCodes, mTypeCode)  =   - 1 )
        
End Function

基本数据间的比较,目前实现了以下操作,Where除外:

     Public   Enum  CompareOperate
        Equal
        NotEqual
        Greater
        GreaterEqual
        Less
        LessEqual
        [Like]
        NotLike
        Between
        NotBetween
        [
In ]
        NotIn
        Where
    
End Enum

比较是基础,筛选是目的。下面举例一下应用。

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mArray  As   Integer ()  =   New   Integer () { 1 101 1001 10001 101 101010101 }

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of   Integer )

        mFilter.Find(mArray, LzmTW.CompareOperate.Between, 
1 1000 )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As   Integer )
        Console.WriteLine(value)
    
End Sub

 

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mArray  As   Integer ()  =   New   Integer () { 1 101 1001 10001 101 101010101 }

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of   Integer )

        mFilter.Find(mArray, LzmTW.CompareOperate.Like, 
" *01 " )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As   Integer )
        Console.WriteLine(value)
    
End Sub

 

    Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mArray  As   Integer ()  =   New   Integer () { 1 101 1001 10001 101 101010101 }

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of   Integer )

        mFilter.Find(mArray, LzmTW.CompareOperate.NotIn, 
1 101 1001 )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As   Integer )
        Console.WriteLine(value)
    
End Sub

 稍复杂些:

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mMenuMamager  As   New  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu.MenuManager  ' 这是一个菜单管理类
        mMenuMamager.GetDefault()  ' 取默认菜单

        
Dim  mTable  As   New  DataTable
        mMenuMamager.UpdateTo(mTable) 
' 将默认菜单的数据输出到mTable

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of  DataRow)

        
' 取列名为Name,值在a与c之间的行
        mFilter.Find(mTable.Rows,  " Item " " Name " , LzmTW.CompareOperate.Between,  " a " " c " )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As  DataRow)
        Console.WriteLine(value.Item(
" Name " ))
    
End Sub

也可以这样取,列Name在Columns中的序号为9

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mMenuMamager  As   New  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu.MenuManager  ' 这是一个菜单管理类
        mMenuMamager.GetDefault()  ' 取默认菜单

        
Dim  mTable  As   New  DataTable
        mMenuMamager.UpdateTo(mTable) 
' 将默认菜单的数据输出到mTable

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of  DataRow)

        
' 取列名为Name,值在a与c之间的行
        mFilter.Find(mTable.Rows,  " Item " 9 , LzmTW.CompareOperate.Between,  " a " " c " )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As  DataRow)
        Console.WriteLine(value.Item(
" Name " ))
    
End Sub

还可以这样取:

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mMenuMamager  As   New  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu.MenuManager  ' 这是一个菜单管理类
        mMenuMamager.GetDefault()  ' 取默认菜单

        
Dim  mTable  As   New  DataTable
        mMenuMamager.UpdateTo(mTable) 
' 将默认菜单的数据输出到mTable

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of  DataRow)

        
' 取列名为Name,值在a与c之间的行
        mFilter.Find(mTable.Rows,  " ItemArray " 9 , LzmTW.CompareOperate.Between,  " a " " c " )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As  DataRow)
        Console.WriteLine(value.Item(
" Name " ))
    
End Sub

 如果是自身具有同类迭代器:

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mMenuMamager  As   New  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu.MenuManager  ' 这是一个菜单管理类
        mMenuMamager.GetDefault()  ' 取默认菜单

        mMenuMamager.UpdateTo(
Me .TreeView1,  " Name " ' 将默认菜单的数据作为树节点输出到TreeView1,节点的Text为菜单Name值

        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of  TreeNode)

        
' 取节点Text值在a与c之间的节点
        mFilter.Find( Me .TreeView1.Nodes( 0 ),  " Text " , LzmTW.CompareOperate.Between,  " a " " c " )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As  TreeNode)
        Console.WriteLine(value.Text)
    
End Sub

 

     Private   Sub  Button1_Click( ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  Button1.Click
        
Dim  mFilter  As   New  LzmTW.uSystem.uCollection.SimpleFilter( Of  Control)

        
' 取窗体的所有控件,包括不属于窗本控件容器而属于其它控件空器的控件
        mFilter.Find( Me " Name " , LzmTW.CompareOperate.NotEqual,  "" )

        mFilter.ForEach(
AddressOf  ResultAction)
    
End Sub

    
Private   Sub  ResultAction( ByVal  value  As  Control)
        Console.WriteLine(value.Name)
    
End Sub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值