Last post Sep 19, 2014 05:33 AM by Fei Han - MSFT
Sep 18, 2014 12:30 PM|mz1378|LINK
I have linqkit and want to build a predicate containing parenthesis, I am now something like this that is not working properly:
else if (key.ToLower() == "brand")
List<string> values = kvFilters.First(kv => kv.Key.ToLower() == "brand").Value.ToList<string>();
int counter = 0;
foreach (string brand in values)
if (counter == 0)
products = products.And(prod => prod.Brand == brand);
products = products.Or(prod => prod.Brand == brand && prod.Enabled && prod.CategoryID == categoryID);
At the beginning of a parenthesis I inject an AND and the following expressions with OR. I think the predicate it creates is:
where(A && B || C || D ...)
that is not correct because I want the following expressions wrapped in parenthesis something that I want is:
where(A && (B || C || D ...))
How do I do this.
Sep 19, 2014 05:33 AM|Fei Han - MSFT|LINK
Thanks for your post.
You could refer to the following sample which describes how to deal with the parenthesis around the expressions.
p => p.A == A && ((p => p.B == B || (p => p.B == B)||(p => p.B == B))
var inner = PredicateBuilder.False<products>();
inner = inner.Or (p => p.B == B);
inner = inner.Or (p => p.C == C);
inner = inner.Or (p => p.D == D);
var outer = PredicateBuilder.True<products>();
outer = outer.And (p => p.A == A);
outer = outer.And (inner);
Hope it will be helpful to you.