[TestMethod]
public void DynamicQueryCombiningPredicates() {
//T11526
var pd1 = new PredicateDescription("UnitPrice", FilterOperator.IsGreaterThanOrEqualTo, 24);
var pd2 = new PredicateDescription("Discontinued", FilterOperator.IsEqualTo, true);
var pd3 = new PredicateDescription("Category.Name", FilterOperator.StartsWith, "P");
var arrPredicates = new ArrayList { pd1, pd2, pd3 };
CompositePredicateDescription compPredicates = null;
IPredicateDescription predicate = null;
for (int arrCount = 0; arrCount < arrPredicates.Count; arrCount++) {
if (predicate != null) {
if (compPredicates == null) {
compPredicates = predicate.And((IPredicateDescription)arrPredicates[arrCount]);
//tried this as well
//compPredicates = (predicate.And((IPredicateDescription)arrPredicates[arrCount]));
} else {
//here is the part where i add the parenthesis, but i tried the above as well...
compPredicates = (compPredicates).Or((IPredicateDescription)arrPredicates[arrCount]);
}
} else {
predicate = (IPredicateDescription)arrPredicates[arrCount];
}
}
var r1 = _em1.Products.Where(p => (p.UnitPrice >= 24 && p.Discontinued) || p.Category.Name.StartsWith("P")).ToList();
var r2 = _em1.Products.Where(compPredicates).ToList();
Assert.IsTrue(r1.Count == r2.Count);
}