User selectable search

Posts   
 
    
Posts: 28
Joined: 27-Mar-2007
# Posted on: 06-Mar-2008 15:49:41   

Hi, I am in the process of creating a user selectable search feature. This allows the user to select a field from a drop down list and type some text in text box. Clicking the search button begins the process of finding a record that matches or is the nearest match. Having found the record I need to find its offset in a datasource and convert this to a page/offset. The record key and page/offset are used to 'jump' to the correct page and set the details record. I started with TypedLists as the means to define the set of fields to search. I pass the TypedListBase2 to a user control which extracts the column names for the drop down. This means I only need to gen code if I change the columns to search. I can detect types from the DataType for each column and so determine numeric or alphanumeric searches. The tricky part is formulating the search using the TypedListBase2 class. The data sets to search can be quite large ( several hundred thousand records ) so I was reluctant to read them into memory an do matches. I wondered if there were any ideas as to the best approach.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Mar-2008 17:13:26   

I don't clearly understand you. Anyway, you can fetch a TypedList directly from DB passing a IPredicateExpression, so you don't have to do it in-memory (find matches). I post a tiny example here:


public void FillSomeTypedList()
{
    // the list
    ProductTypedListTypedList productList = new ProductTypedListTypedList();

    // the filter
    IRelationPredicateBucket filter = BuildTheFilter(productList);

    // the results
    productList = (ProductTypedListTypedList) FetchResults(productList, filter);

    ...

}

private IRelationPredicateBucket BuildTheFilter(ITypedListLgp2 list)
{
    // fields
    IEntityFields2 fields = list.GetFieldsInfo();

    /// selected field
    /// Here you have to extract the alias name from the combobox
    string selectedFieldFromCombo = "CompanyName";
    EntityField2 selectedField =  (EntityField2) fields[selectedFieldFromCombo];

    /// value to search
    /// Here you have to extract the value from a TextBox.Text
    string searchValue = "Karkki Oy";
    object castedValue = Convert.ChangeType(searchValue, selectedField.DataType);

    /// build the filter
    /// This is the same in all cases. Unless you want to do various comparissions (==, <, >, etc).
    IRelationPredicateBucket filter = new RelationPredicateBucket();
    filter.PredicateExpression.Add(selectedField == castedValue);

    // lets return the filter
    return filter;
}

private ITypedListLgp2 FetchResults(ITypedListLgp2 list, IRelationPredicateBucket filter)
{
    // fetch the typedList
    using (DataAccessAdapter adapter = new DataAccessAdapter())
    {
        adapter.FetchTypedList(list, filter.PredicateExpression);
    }

    // return results
    return list;
}

I don't get you about the page/offset thing...

cheers

David Elizondo | LLBLGen Support Team
Posts: 28
Joined: 27-Mar-2007
# Posted on: 06-Mar-2008 18:02:21   

Sorry about the confusion. I am trying to develop a general search user control that allows the user to select a field and specify some text. There are several pages each of which has a gridview and a details view. I use a typedlist as the data source with some of the columns exposed in the gridview. The typelist provides a means to lookup foriegn keys and give columns sensible names. When the search button is clicked, the search facility would then lookup the nearest matching record and from this deduce which page the gridview needed to be selected so as to show the selected item and thus populate the details view. ( PageIndex and SelectedIndex)

The initial design was to pass a typedlist to the search control as a generic type(TypedListBase2) and process the search from there. So far I can get a list of columns and their types, but have come unstuck formulating the predicate ( the GetRelationInfo is specific and not at the TypedListBase2 level). Given that the search button can be clicked , anytime after the typelist is set up, I need to store these items ( ViewState?). As an alternative, I was thinking of storing the querry string, appending different where clauses dependant on the search criteria.

Interesting code. Will experiment.

Thanks