Billy Logan
Billy Logan

Reputation: 2863

Entity Framework using Predicates

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

Upvotes: 1

Views: 677

Answers (1)

Jehof
Jehof

Reputation: 35544

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')));

Upvotes: 1

Related Questions