Expression.Condition Methode

Definitie

Hiermee maakt u een ConditionalExpression voorwaardelijke instructie.

Overloads

Name Description
Condition(Expression, Expression, Expression)

Hiermee maakt u een ConditionalExpression voorwaardelijke instructie.

Condition(Expression, Expression, Expression, Type)

Hiermee maakt u een ConditionalExpression voorwaardelijke instructie.

Condition(Expression, Expression, Expression)

Hiermee maakt u een ConditionalExpression voorwaardelijke instructie.

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

Een Expression om de Test eigenschap in te stellen die gelijk is aan.

ifTrue
Expression

Een Expression om de IfTrue eigenschap in te stellen die gelijk is aan.

ifFalse
Expression

Een Expression om de IfFalse eigenschap in te stellen die gelijk is aan.

Retouren

Een ConditionalExpression met de NodeType eigenschap gelijk aan Conditional en de Test, IfTrueen IfFalse eigenschappen die zijn ingesteld op de opgegeven waarden.

Uitzonderingen

test ifFalse of ifTrue is null.

test. Type is niet Boolean.

– of –

ifTrue. Het type is niet gelijk aan ifFalse. Type.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe u een expressie maakt die een voorwaardelijke instructie vertegenwoordigt. Als het eerste argument wordt geëvalueerd true, wordt het tweede argument uitgevoerd. Anders wordt het derde argument uitgevoerd.

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

Opmerkingen

De Type eigenschap van het resulterende ConditionalExpression is gelijk aan de Type eigenschap van ifTrue.

Zie ook

Van toepassing op

Condition(Expression, Expression, Expression, Type)

Hiermee maakt u een ConditionalExpression voorwaardelijke instructie.

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

Een Expression om de Test eigenschap in te stellen die gelijk is aan.

ifTrue
Expression

Een Expression om de IfTrue eigenschap in te stellen die gelijk is aan.

ifFalse
Expression

Een Expression om de IfFalse eigenschap in te stellen die gelijk is aan.

type
Type

A Type om de Type eigenschap in te stellen die gelijk is aan.

Retouren

Een ConditionalExpression met de NodeType eigenschap gelijk aan Conditional en de Test, IfTrueen IfFalse eigenschappen die zijn ingesteld op de opgegeven waarden.

Opmerkingen

Met deze methode kunt u het resultaattype van de voorwaardelijke expressie expliciet combineren in gevallen waarin de typen ifTrue en ifFalse expressies niet gelijk zijn. Typen beide ifTrue en ifFalse moeten impliciet worden verwezen naar het resultaattype. Het type is toegestaan.Void

Van toepassing op