Expression.Condition Méthode

Définition

Crée un ConditionalExpression qui représente une instruction conditionnelle.

Surcharges

Condition(Expression, Expression, Expression)

Crée un ConditionalExpression qui représente une instruction conditionnelle.

Condition(Expression, Expression, Expression, Type)

Crée un ConditionalExpression qui représente une instruction conditionnelle.

Condition(Expression, Expression, Expression)

Crée un ConditionalExpression qui représente une instruction conditionnelle.

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

Paramètres

test
Expression

Expression auquel la propriété Test doit être égale.

ifTrue
Expression

Expression auquel la propriété IfTrue doit être égale.

ifFalse
Expression

Expression auquel la propriété IfFalse doit être égale.

Retours

ConditionalExpression

ConditionalExpression dont la propriété NodeType est égale à Conditional et dont les propriétés Test, IfTrue et IfFalse ont les valeurs spécifiées.

Exceptions

test ou ifTrue ou ifFalse a la valeur null.

test.Type n'est pas Boolean.

  • ou - ifTrue.Type n'est pas égal à ifFalse.Type.

Exemples

L’exemple de code suivant montre comment créer une expression qui représente une instruction conditionnelle. Si le premier argument est trueévalué à , le deuxième argument est exécuté ; sinon, le troisième argument est exécuté.

// 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

Remarques

La Type propriété du résultat ConditionalExpression est égale à la Type propriété de ifTrue.

Voir aussi

S’applique à

Condition(Expression, Expression, Expression, Type)

Crée un ConditionalExpression qui représente une instruction conditionnelle.

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

Paramètres

test
Expression

Expression auquel la propriété Test doit être égale.

ifTrue
Expression

Expression auquel la propriété IfTrue doit être égale.

ifFalse
Expression

Expression auquel la propriété IfFalse doit être égale.

type
Type

Type auquel la propriété Type doit être égale.

Retours

ConditionalExpression

ConditionalExpression dont la propriété NodeType est égale à Conditional et dont les propriétés Test, IfTrue et IfFalse ont les valeurs spécifiées.

Remarques

Cette méthode permet d’unifier explicitement le type de résultat de l’expression conditionnelle dans les cas où les types et ifTrue ifFalse expressions ne sont pas égaux. Les types des deux ifTrue et ifFalse doivent être implicitement assignables au type de résultat. Il type est autorisé à être Void.

S’applique à