Filtering a Custom Collection of business Objects - C#

E

Erik

I have a base collection class that I use to derive all my collections
from. It derives from CollectionBase. I have added custom Sorting and
now would like to add Filtering.

I have spent a decent amount of time looking thru code and really have
not found anything that suits my needs. I simply want to pass in a
filter expression use that that expression against the protected List
(exposed by CollectionBase) object and return an array list containing
only the objects that meet that filter expression.

Is this possible? OR Shall I just do forEach and check the props of
the objects I have stored in the Collection?

Any help would be great.

Erik
 
I

Ignacio Machin

Hi Erik

I have created a set of classes for this, it's very easy using reflection.
Just give me a few hours to prepare you a document with the code and an
explanation of how to use it.

You use reflection, and you pass the property/method name you will filter
by and it does not require any modification in the classes being filtered .

Cheers,
 
Joined
May 25, 2006
Messages
1
Reaction score
0
Hi

I am trying to do the same. Can you post the code? Thank you very much in advance.

Madhu
 
Joined
Jun 1, 2010
Messages
6
Reaction score
0
Sorting a user defined collection class

This tip demonstrates how to sort a user-defined collection class. Let us say we have a custom class as ClientCollection (collection of Client object) which Inherits System.Collections.CollectionBase. To sort the collection , order by LAST NAME , we can use the following steps.

First create a class that implements [IComparer] interface and define the [Compare] method.Following is the class that we will use for sorting Clients order by last name.
[VB.NET CODE STARTS]

Public Class ClientComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim objClientX As Client = CType(x, Client) ' Client is the class having FirtsName, LastName as properties
Dim objClientY As Client = CType(y, Client)

Dim sX As String = UCase(objClientX.LastName) ' comparision is made using the LAST NAME OF CLIENT
Dim sY As String = UCase(objClientY.LastName)

If sX = sY Then
Return 0
End If

If sX.Length > sY.Length Then
sX = sX.Substring(0, sY.Length)
If sX = sY Then
Return 1
End If
ElseIf sX.Length < sY.Length Then
sY = sY.Substring(0, sX.Length)
If sX = sY Then
Return -1
End If
End If

For i As Integer = 0 To sX.Length
If Not sX.Substring(i, 1) = sY.Substring(i, 1) Then
Return Asc(CType(sX.Substring(i, 1), Char)) - Asc(CType(sY.Substring(i, 1), Char))
End If
Next
End Function

End Class

[VB.NET CODE ENDS]

Any Suggestions are appreciated.

http://www.mindfiresolutions.com/Sorting-a-user-defined-collection-class-877.php
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top