1

I am in the process of building out a search that requires the use of predicates. However, whenever i run the below query i continue to get only the results of the last predicate. The results from the first are not included. Based off my query below can anyone point me in the right direction.

var _predicate = PredicateBuilder.False<TBLDESIGN>();

_predicate = _predicate.Or(a =>  (a.KEYWORDS.Contains('red') || a.NAME.Contains('red')));

_predicate = _predicate.Or(a =>  (a.KEYWORDS.Contains('blue') || a.NAME.Contains('blue')));


                var results = dbContext.TBLDESIGN
                            .Include(s => s.TBLCOLLECTION)
                            .Include(s => s.LKPRICE)
                            .Include(s => s.TBLDESIGNER)
                            .AsExpandable().Where(_predicate)
                            .OrderByDescending(s => s.DATE_APPROVED)
                            .Select(s => new
                            {
                                s.ACTIVE,
                                s.DATE_CREATED,
                                s.EXPORTED,
                                s.IMAGE_PATH,
                                DesignId = s.ID,
                                s.IS_APPROVED,
                                s.TBLDESIGNER.FIRST_NAME,
                                s.TBLDESIGNER.LAST_NAME,
                                s.TBLDESIGNER.ALIAS,
                                s.NAME,
                                s.LKPRICE.PRICE,
                                s.COMPLETED,
                                s.DATE_APPROVED,
                                DesignerId = s.TBLDESIGNER.ID,
                                s.VIEW_COUNT
                            }).ToList();

This query is supposed to pull back any designs that have a keyword containing red or name containing red or keyword containing blue or name containing blue.

Currently under this scenario it ignores the first predicate and just returns the blue values.

Thanks for your help, Billy

Billy Logan
  • 2,833
  • 16
  • 43
  • 49

1 Answers1

1

Maybe this isn´t the solution, but do you tried to split the predicates as follows

var _predicate = PredicateBuilder.False<TBLDESIGN>();

_predicate = _predicate.Or(a => a.KEYWORDS.Contains('red'));
_predicate = _predicate.Or(a => a.NAME.Contains('red')));
_predicate = _predicate.Or(a => a.KEYWORDS.Contains('blue'));
_predicate = _predicate.Or(a => a.NAME.Contains('blue')));
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • no, i have tried a few variations but not that spread out. I will give it a shot and let you know. Thanks for the reply. – Billy Logan Feb 17 '12 at 15:34
  • This does appear to move me further in the right direction, but for whatever reason if keywords is empty and name still contains 'red' that record is not returned?? – Billy Logan Feb 17 '12 at 16:42
  • I figured out my issue. It wasn't the predicate that was the issue. It was the fact that TBLCOLLECTION should be an outer join. After removing that both of our examples work the same. So i will give you the answer since that is a solution. Thanks, Billy – Billy Logan Feb 17 '12 at 16:51