Using the built-in collection class IndexedCollection<T> (LiveLinq to Objects)

Suppose first that we don't care what collection we use for our objects. We have a Customer class, something like

 

public class Customer

{

    public string Name { get; set; }

    public string City { get; set; }

}

 

and we rely on LiveLinq to supply the collection class we need.

Then we need look no further than the C1.LiveLinq.Collections.IndexedCollection class that is supplied by LiveLinq and is specifically optimized for LiveLinq use:

 

var customers = new IndexedCollection<Customer>();

customers.Add(cust1);

customers.Add(cust2);

...

var query =

  from c in customers where c.City == "London"select c;

 

Note that we can simply use customers instead of customers.AsIndexed(). It is because the IndexedCollection<T> class already implements the IIndexedSource<T> interface that LiveLinq needs, there is no need to use the AsIndexed() extension method to wrap it into that interface.

There is an important consideration to be kept in mind using your own classes such as Customer above for collection elements. The Customer class above is so basic that it does not support property notifications. If you set one of its properties in code, nobody will notice it, including any indexes and live views you may create over that collection. Therefore it is highly necessary to provide property change notifications in such classes. Property change notifications is a standard .NET feature recommended for a variety of reasons, LiveLinq just adds another reason to do that. You can support property change notifications in your class by implementing the INotifyPropertyChanging and INotifyPropertyChanged interfaces. Or you can use LiveLinq for that, by deriving your class from IndexableObject and calling OnPropertyChanging/OnPropertyChanged like this:

 

public class Customer : IndexableObject

{

    private string _name;

    public string Name

    {

        get {return _name} ;

        set

        {

            OnPropertyChanging("Name");

            _name = value;

            OnPropertyChanged("Name");

        }

    }

    private string _city;

    public string City

    {

        get {return _city};

        set

        {

            OnPropertyChanging("City");

            _city = value;

            OnPropertyChanged("City");

        }

    }

}


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.