Filtering the list items

Now that we have an IndexedList of type Person with many items (in Create and add Indexes page), and we want to do search the people with name and family :

IndexedList uses indexes in 2 ways:
1) IndexedList decides witch index to use for search.
2) We tell the index to use witch Index (by Index name).

Usually we will use the first option. We should use the second option just in one condition :
We have 2 or more indexes with same number and data types of fields
For exapmle we have 2 indexes first on Name and Family fields and second on Name and NationalNo fields, so both indexes are on 2 string fields.
To do search on items you can use each of these 4 methods :


This method has 2 overloads :
1) Expression :
people.Find(p => p.Name == "Alex" && p.Family == "Mahoon");

This is very clear and nice way but as i have tested creating an Expression (LambdaExpression) has some overheads and of course parsing an expression has overhead too. So using expressions is a good way but unfortunately in compare with other ways is slower.


I this way Indexed list will find an index witch has 2 string fields(if there was 2 or more indexes with 2 string fields an exception will be thrown), then do search by using that index.
Note:you should be careful about the ordering of fields, the values should be ordered the same as they are used in index creation
Consider the fallowing example :
IIndexedList<Person> people = new IndexedList<Person>();
ListIndex<Person> index = new ListIndex<Person>(p=> new {p.Name, p.Family });
//add people from datasource

The above example will work but the result is the list of people with name of "Mahoon" and family of "Alex" because the order of fields in index declaration is Name then Family


In this method you should specify the name of index you want to use,
This method has 2 overloads too:
1) Using IndexName and a similar item:
IIndexedList<Person> people = new IndexedList<Person>();
//Add index with name of "Idx_1"
ListIndex<Person> index = new ListIndex<Person>("Idx_1", p=> new {p.Name, p.Family });
//add people from datasource
people.FindUsingIndex("Idx_1", new Person() { Id=1001, Name = "Alex", Family = "Mahoon" });

This will find all People with Name of "Alex" and Family of "Mahoon".
Note that Id field is not considered anymore because Idx_1 just have Name and Family fields

2)Using IndexName and fieldvalues :
people.FindUsingIndex("Idx_1", "Alex" , "Mahoon" );

This is just like Find method with FieldValues parameters the only difference is that the index is specified
(in this example if there are 2 or more indexes with 2 string fields there will be no problem)

FindSingle and FindSingleUsingIndex are same as above method but just returns one single item instead of a list.

Last edited Oct 15, 2013 at 2:31 PM by Basir_ansary, version 7