Question

I am using predicates to refine search options and am getting two different results when i run what looks to be the same query.

This one is correct and returns the expected results:(hard coded strings)

_predicate = PredicateBuilder.False<TBLDESIGN>();
_predicate = _predicate.Or(a => a.IS_APPROVED == true & a.ACTIVE == true & a.DATE_APPROVED > beginDate & (a.KEYWORDS.Contains("red") || a.NAME.Contains("red")));
_predicate = _predicate.Or(a => a.IS_APPROVED == true & a.ACTIVE == true & a.DATE_APPROVED > beginDate & (a.KEYWORDS.Contains("geos") || a.NAME.Contains("geos")));

This one only returns the results from the second predicate "geos". The first one in the loop("red") is ignored. Values "red" and "geos" are inside the loop below.

string searchTerm = "";
_predicate = PredicateBuilder.False<TBLDESIGN>();
for (int i = 0; i < search.Count(); i++)
{
    searchTerm = search[i].SearchTerm.Trim().ToString();
    _predicate = _predicate.Or(a => a.IS_APPROVED == true & a.ACTIVE == true & a.DATE_APPROVED > beginDate & (a.KEYWORDS.Contains(searchTerm) || a.NAME.Contains(searchTerm)));
}

Here is the rest of the query if that makes any difference.

            var results =
                        dbContext.TBLDESIGN
                        .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();

Question is how to get the second list of predicates to return the same results as the first series. Guessing there is something extra to do when you are passing a string variable in rather than a hard coded value.

Thanks, Billy

Answer:

_predicate = PredicateBuilder.False<TBLDESIGN>();
for (int i = 0; i < search.Count(); i++)
{
    string searchTerm = search[i].SearchTerm.Trim().ToString();
    _predicate = _predicate.Or(a => a.IS_APPROVED == true & a.ACTIVE == true & a.DATE_APPROVED > beginDate & (a.KEYWORDS.Contains(searchTerm) || a.NAME.Contains(searchTerm)));
}
Était-ce utile?

La solution

string declaration needs to occur inside of the loop.

_predicate = PredicateBuilder.False<TBLDESIGN>();
    for (int i = 0; i < search.Count(); i++)
    {
        string searchTerm = search[i].SearchTerm.Trim().ToString();
        _predicate = _predicate.Or(a => a.IS_APPROVED == true & a.ACTIVE == true & a.DATE_APPROVED > beginDate & (a.KEYWORDS.Contains(searchTerm) || a.NAME.Contains(searchTerm)));
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top