How can I implement “and”/”Or” function for "IF/THEN" statement?

Mar 7, 2011 at 5:12 AM

 Hello Guys,

Firstly I’d like to say the test API library is really useful for our job and really appreciate it.

Recently I’m writing a GUI application based on Combinatorial Variation Generation APIs, but I’ve met a big problem for “IF/THEN “ function.

Currently the API implement if/then function as below:

Constraint<Variation>

       .If(v => de.GetValue(v) == "Las Vegas" || de.GetValue(v) == "Hawaii")

       .Then(v => ac.GetValue(v) != "skiing"),

 

I want to implement below function such as:

Expression<Func<Variation, bool>> Destination1 = v => de.GetValue(v) == "Las Vegas";

Expression<Func<Variation, bool>> Destination2 = de.GetValue(v) == "Hawaii";

Expression<Func<Variation, bool>> Destination3 = Destination1.Or(Destination2);

How can I implement “and”/”Or” function?

I’m looking forward to your reply.

Thank you in advance.

 

 

Mar 7, 2011 at 4:48 PM

If I follow your question, I think this should work:

Constraint<Variation

>  

   .Conditional(v => destination.GetValue(v) == "Las Vegas" || destination.GetValue(v) == "Hawaii" 

)

 

You can use the OM provided by the Expression class (http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.aspx) to manipulate the expressions you pass to the constraints, but that shouldn't be necessary except in more complicated scenarios.

Hope this helps.

 

 

Mar 8, 2011 at 7:53 AM

Hi NathanA,

Thank you for your quick respond!

But maybe I need to re-describe my problem.

Currently the API implement if function as below:

Constraint<Variation>

       .If(v => de.GetValue(v) == "Las Vegas" || de.GetValue(v) == "Hawaii")

I want to implement below function such as:

Expression<Func<Variation, bool>> Destination1 = v => de.GetValue(v) == "Las Vegas";

Expression<Func<Variation, bool>> Destination2 = v => de.GetValue(v) == "Hawaii";

Expression<Func<Variation, bool>> Destination3 = Destination1.Or(Destination2);

Please note the red mark

For your given link, it just provides Expression.And Method, but it doesn't provide And method for Expression<Func<Variation, bool>>

Thank you again.

Mar 8, 2011 at 5:11 PM

For the most part you shouldn't need to use the System.Linq.Expression OM, but it is possible to use it though it will be difficult.  This seems to be more of LINQ question than a TestApi question, the LINQ forum may be able to provide you with more help:  http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/threads/

A few pointers:

  • I think you should be using AndAlso/OrElse.  Or and And are the bitwise operators.
  • This will create an Expression<Func<Variation,bool>>:

Expression<Func<Variation, bool>> e1 = v => destination.GetValue(v) == "Las Vegas";
Expression<Func<Variation, bool>> e2 = v => destination.GetValue(v) == "Hawaii";
Expression<Func<Variation, bool>> combined = Expression.Lambda<Func<Variation, bool>>(Expression.OrElse(e1.Body, e2.Body), Expression.Parameter(typeof(Variation), "v"));

Note you won't be able to call this however, the ParameterExpression in e1, e2 and combined all have to refer to the same ParemeterExpression object.  An expression visitor can help with this:

http://msdn.microsoft.com/en-us/library/bb882521.aspx

Derive from this class and override VisitParameter and have it return the common parameter.  Then have it Visit e1 and e2.  The results will share a common parameter.

Hope this helps.