Επεξεργασία

Κοινή χρήση μέσω


Expression.Condition Method

Definition

Creates a ConditionalExpression that represents a conditional statement.

Overloads

Condition(Expression, Expression, Expression)

Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression, Type)

Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

Creates a ConditionalExpression that represents a conditional statement.

public:
 static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression) As ConditionalExpression

Parameters

test
Expression

An Expression to set the Test property equal to.

ifTrue
Expression

An Expression to set the IfTrue property equal to.

ifFalse
Expression

An Expression to set the IfFalse property equal to.

Returns

A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

Exceptions

test or ifTrue or ifFalse is null.

test.Type is not Boolean.

-or-

ifTrue.Type is not equal to ifFalse.Type.

Examples

The following code example shows how to create an expression that represents a conditional statement. If the first argument evaluates to true, the second argument is executed; otherwise, the third argument is executed.

// Add the following directive to your file:
// using System.Linq.Expressions;

int num = 100;

// This expression represents a conditional operation.
// It evaluates the test (first expression) and
// executes the iftrue block (second argument) if the test evaluates to true,
// or the iffalse block (third argument) if the test evaluates to false.
Expression conditionExpr = Expression.Condition(
                           Expression.Constant(num > 10),
                           Expression.Constant("num is greater than 10"),
                           Expression.Constant("num is smaller than 10")
                         );

// Print out the expression.
Console.WriteLine(conditionExpr.ToString());

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
Console.WriteLine(
    Expression.Lambda<Func<string>>(conditionExpr).Compile()());

// This code example produces the following output:
//
// IIF("True", "num is greater than 10", "num is smaller than 10")
// num is greater than 10
' Add the following directive to your file:
' Imports System.Linq.Expressions  

Dim num As Integer = 100

' This expression represents a conditional operation; 
' it will evaluate the test (first expression) and
' execute the ifTrue block (second argument) if the test evaluates to true, 
' or the ifFalse block (third argument) if the test evaluates to false.
Dim conditionExpr As Expression = Expression.Condition(
                            Expression.Constant(num > 10),
                            Expression.Constant("n is greater than 10"),
                            Expression.Constant("n is smaller than 10")
                        )

' Print the expression.
Console.WriteLine(conditionExpr.ToString())

' The following statement first creates an expression tree,
' then compiles it, and then executes it.       
Console.WriteLine(
    Expression.Lambda(Of Func(Of String))(conditionExpr).Compile()())

' This code example produces the following output:
'
' IIF("True", "num is greater than 10", "num is smaller than 10")
' num is greater than 10

Remarks

The Type property of the resulting ConditionalExpression is equal to the Type property of ifTrue.

See also

Applies to

Condition(Expression, Expression, Expression, Type)

Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs
Source:
ConditionalExpression.cs

Creates a ConditionalExpression that represents a conditional statement.

public:
 static System::Linq::Expressions::ConditionalExpression ^ Condition(System::Linq::Expressions::Expression ^ test, System::Linq::Expressions::Expression ^ ifTrue, System::Linq::Expressions::Expression ^ ifFalse, Type ^ type);
public static System.Linq.Expressions.ConditionalExpression Condition (System.Linq.Expressions.Expression test, System.Linq.Expressions.Expression ifTrue, System.Linq.Expressions.Expression ifFalse, Type type);
static member Condition : System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * System.Linq.Expressions.Expression * Type -> System.Linq.Expressions.ConditionalExpression
Public Shared Function Condition (test As Expression, ifTrue As Expression, ifFalse As Expression, type As Type) As ConditionalExpression

Parameters

test
Expression

An Expression to set the Test property equal to.

ifTrue
Expression

An Expression to set the IfTrue property equal to.

ifFalse
Expression

An Expression to set the IfFalse property equal to.

type
Type

A Type to set the Type property equal to.

Returns

A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

Remarks

This method allows explicitly unifying the result type of the conditional expression in cases where the types of ifTrue and ifFalse expressions are not equal. Types of both ifTrue and ifFalse must be implicitly reference assignable to the result type. The type is allowed to be Void.

Applies to