Predicate Search

Posts   
 
    
RickK
User
Posts: 3
Joined: 18-Feb-2005
# Posted on: 18-Feb-2005 05:13:10   

Absolutely love the product, but I'm having a bit of an issue trying to search using Predicates and could use a little input.

I've got 2 tables: Issues and IssuesKeywords where IssuesKeywords has a foreign key to Issues of IDIssue. User optionally selects a Category and inputs Keywords to search against and I'd like to return the Issues for those keywords.

The SQL I'm getting in IssuesDAO (I'm using Self-Servicing) base.ExecuteMultiRowRetrievalQuery in the selectQuery command looks like:


SELECT  [Issues].[IDIssue] AS [Idissue],
    [Issues].[ShortDescription] AS [ShortDescription],
    [Issues].[IDCategory] AS [Idcategory],
    [Issues].[Resolution] AS [Resolution] 
FROM  ( [Issues] 
    INNER JOIN [IssuesKeywords] ON  
                  [Issues].[IDIssue]=[IssuesKeywords].[IDIssue]) 
WHERE ( [Issues].[IDCategory] = @Idcategory1 And [IssuesKeywords].[Keyword] LIKE @Keyword2 
    Or [Issues].[IDCategory] = @Idcategory3 And [IssuesKeywords].[Keyword] LIKE @Keyword4)

The WHERE clause I'd like to get to functionally is:


WHERE Issues.IDCategory = x and (IssuesKeywords.Keyword Like @keyword1 or IssuesKeywords.Keyword Like @keyword2)

I may be working to hard to get the right select filter but this is where I'm at - the category is optionally specified by the user so it might not be there:


string [] split = this.txtSearchText.Text.Split(new Char [] {'|'});
foreach (string s in split)
{
    if (selectFilter.Count == 0)
    {
        if (IDCategory != -1)
        {
            selectFilter.Add(DAL.FactoryClasses.PredicateFactory.CompareValue(DAL.IssuesFieldIndex.Idcategory, ORM.ComparisonOperator.Equal, IDCategory));
            selectFilter.AddWithAnd(DAL.FactoryClasses.PredicateFactory.Like(DAL.IssuesKeywordsFieldIndex.Keyword, "%" + s + "%"));
        }
        else
            selectFilter.Add(DAL.FactoryClasses.PredicateFactory.Like(DAL.IssuesKeywordsFieldIndex.Keyword, "%" + s + "%"));
    }
    else
        if (IDCategory != -1)
        {
            selectFilter.AddWithOr(DAL.FactoryClasses.PredicateFactory.CompareValue(DAL.IssuesFieldIndex.Idcategory, ORM.ComparisonOperator.Equal, IDCategory));
            selectFilter.AddWithAnd(DAL.FactoryClasses.PredicateFactory.Like(DAL.IssuesKeywordsFieldIndex.Keyword, "%" + s + "%"));
        }
        else
            selectFilter.AddWithOr(DAL.FactoryClasses.PredicateFactory.Like(DAL.IssuesKeywordsFieldIndex.Keyword, "%" + s + "%"));
}
            

I think I'm close but I'm just off. Any help or hints would really be appreaciated!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Feb-2005 09:18:15   

I think this will work:


PredicateExpression keywordFilter = new PredicateExpression();
if (IDCategory != -1)
{
    selectFilter.Add(
        DAL.FactoryClasses.PredicateFactory.CompareValue(
            DAL.IssuesFieldIndex.Idcategory, ORM.ComparisonOperator.Equal, IDCategory));
}

foreach (string s in split)
{
    keywordFilter.AddWithOr(
                DAL.FactoryClasses.PredicateFactory.Like(DAL.IssuesKeywordsFieldIndex.Keyword, "%" + s + "%"));
}

selectFilter.AddWithAnd(keywordFilter);

You have to add the like predicates to another predicate expression, so they'll be wrapped within '()'.

Frans Bouma | Lead developer LLBLGen Pro