Using Strongly Typed Objects and Collections to replace DataSet’s in your .NET applications.
page 4 of 4
by Phil Winstanley
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27794/ 71

Sorting and Filtering Strongly Typed Collections

Filtering

Whilst you can still use SQL Queries against your Database to perform searches, you can also perform searches against your collections. This is achieved by running you Collection through a filter. It’s easy to set up a Filter Class as s -

public class FilterFoos

{

 

          private double _PriceFrom = -1;

          public double PriceFrom

          {

                   set

                   {

                             _PriceFrom = value;

                   }

                   get

                   {

                             return _PriceFrom;

                   }

          }

 

          private FooCollection _Foos;

          public FooCollection Foos

          {

                   set

                   {

                             _Foos = value;

                   }

          }

 

          public FooCollection Filter()

          {

 

                   FooCollection Results = _Foos;

 

                   //If we have a Minimum Price let's filter the Foos by that

                   if (_PriceFrom > 0)

                   {

                  

                             FooCollection G = new FooCollection();

 

                             foreach(Foo C in Results)

                             {

                                      if(C.Price >= _PriceFrom)

                                      {

                                                G.Add(C);

                                       }

                             }

 

                             Results = G;

 

                   }

 

          return Results;

 

}

 

}

 

The above only has one ”Filter” inside it, but it’s really simple to set up more filters, and the above can be very easily used with your Strongly Typed Objects: -

 

private FooCollection DoFilter(FooCollection CC)

{

 

FilterFoos F = new FilterFoos();

 

if (Request.Form["PriceFrom"] != null)

{

                   F.PriceFrom = Convert.ToDouble(Request.Form["PriceFrom"].ToString());

}

 

//Perform the Filter on Price From

F.Foos = CC;

CC = F.Filter();

 

return CC;

}

 

Sorting

The simplest way of having your Collections sorted is by running the “Order By” clause in your SQL Lookup, and then passing the Rows in to your collection, however you may have in memory collections, and to perform sorting on these you will need to use objects called comparers and the ICompare interface, with a sprinkle of Array.Sort().

A Comparer Class needs to be created for the Properties of your objects that you wish to sort on as follows: -

 

public class FooTitleComparer : IComparer

{

          public int Compare(object x, object y)

          {

                   Foo xFoo = ((Foo)x);

                   Foo yFoo = ((Foo)y);

                   return String.Compare(xFoo.Title,yFoo.Title);

          }

          public FooTitleComparer()

          {

          }

}

 

Then in the Collection a Sort method needs to be created: -

public void Sort(string SortProperty)

{

          if (SortProperty.ToLower() == "title")

          {

                   FooTitleComparer myFooTitleComparer = new FooTitleComparer();

                   this.InnerList.Sort(myFooTitleComparer);

          }

}

 

Usage of the Sort method in your code is very simple to -

 

C.Sort("Title");

Advanced Sorting

public void Sort(string SortProperty)

{

          if (SortProperty.ToLower() == "title")

          {

                   FooTitleComparer myFooTitleComparer = new FooTitleComparer();

                   this.InnerList.Sort(myFooTitleComparer);

          }

}

Whilst the above is very flexible it does not allow us to sort in multiple directions (Ascending and Descending). To achieve this functionality more work has to be done with the Comparer Classes. An article covering this can be found here: -

http://www.c-sharpcorner.com/Code/2002/June/CollectionObgOrdering.asp


View Entire Article

User Comments

Title: Mr.   
Name: Alan
Date: 2007-12-28 6:00:51 AM
Comment:
Another reason to use strongly typed objects over datasets is the size of a dataset... For instance if you are building a distributed app and want to send object over the wire to an app server then the overhead of using datasets is huge just because of their serialization size when comapred with strongly typed objects.

Travel boy this is why strongly typing datasets is not a great idea most of the type. You want to avoid the dependency on datasets too.
Title: what about dynamic connection ?   
Name: Sam
Date: 2006-07-19 5:55:06 PM
Comment:
Hi,
Nice article. But you don't go through connection strings. One problem I have is how to pass DYNAMICALLY a connection string to the dataAdapters at runtime. Say that I have a different connection string for each user, stored in a Session variable, how would I pass that to the xsd each time I want to use of a dataTable ?
Title: UML   
Name: Magnus Andersson
Date: 2005-06-10 4:18:14 AM
Comment:
WowWowWow !

Now i can design strongly typed object(dataset) in the whole project, without XML Schema.
This is great when i working with UML Design, because
ADO.NET object is a mess in UML design.
It is now time to build a automatic UML to dataset Class generator
Title: travelboy   
Name: travelboy
Date: 2005-06-08 12:15:54 PM
Comment:
Would advise looking into typed datasets as oppossed to spending hours defining your own collections.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-12-11 8:06:31 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search