0

I asked this question a couple days ago and got a great response. There's still one thing I'm having trouble with. How to look for false when constructing an Expression Tree.

Here's an example:

query.Where(d => (allCk && d.FacilityId == facilityId) //1.
           ||
          (!allCk && allSelected && d.FacilityId == facilityId && !ids.Contains(d.Id)) //2.
            ||
          (!allCk && !allSelected && ids.Contains(d.Id)) //3.

So far, checking whether d.FacilityId == facilityId and ids.Contains(d.Id) are the easiest parts.

Now, as you can see, depending on the block, the same Booleans are checked for the truthfulness and somewhere else it's the opposite. For instance, in the line 1: success is when allCK == true while in the 2 others, allCK == false.

This is how I wrote the first line.

/* d */
        ParameterExpression pe = Expression.Parameter(typeof(Document), "d");

        /*(allCk && d.FacilityId == facilityId) ==> exp0*/ 
        var facilityParam = Expression.Constant(facilityId);
        var allCkParam = Expression.Constant(allCk);

        Expression facilityIdProp = Expression.Property(pe, "FacilityId");
        Expression facilityIdEql = Expression.Equal(facilityIdProp, facilityParam);

        Expression exp0 = Expression.AndAlso(allCkParam, facilityIdEql);

How do I express my intention that, for instance, the success is when allCK == false? (which includes line 2 and 3 where allCK, allSelected, and ids.Contains() need to return false)

Thanks for helping

Richard77
  • 20,343
  • 46
  • 150
  • 252

1 Answers1

3

You simply need to wrap your value in Expression.Not or Expression.IsFalse().

For example:

Expression.AndAlso(Expression.Not(allCkParam), ...)
Rob
  • 26,989
  • 16
  • 82
  • 98