Expression.Condition Methode

Definition

Erstellt eine ConditionalExpression, die eine bedingte Anweisung darstellt.

Überlädt

Condition(Expression, Expression, Expression)

Erstellt eine ConditionalExpression, die eine bedingte Anweisung darstellt.

Condition(Expression, Expression, Expression, Type)

Erstellt eine ConditionalExpression, die eine bedingte Anweisung darstellt.

Condition(Expression, Expression, Expression)

Quelle:
ConditionalExpression.cs
Quelle:
ConditionalExpression.cs
Quelle:
ConditionalExpression.cs

Erstellt eine ConditionalExpression, die eine bedingte Anweisung darstellt.

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

Parameter

test
Expression

Ein Expression, auf den die Test-Eigenschaft festgelegt werden soll.

ifTrue
Expression

Ein Expression, auf den die IfTrue-Eigenschaft festgelegt werden soll.

ifFalse
Expression

Ein Expression, auf den die IfFalse-Eigenschaft festgelegt werden soll.

Gibt zurück

Ein ConditionalExpression, bei dem die NodeType-Eigenschaft gleich Conditional ist und die Eigenschaften Test, IfTrue und IfFalse auf die angegebenen Werte festgelegt sind.

Ausnahmen

test oder ifTrue oder ifFalse ist null.

test.Type ist nicht Boolean.

- oder -

ifTrue.Type ist ungleich ifFalse.Type.

Beispiele

Das folgende Codebeispiel zeigt, wie Sie einen Ausdruck erstellen, der eine bedingte Anweisung darstellt. Wenn das erste Argument als trueausgewertet wird, wird das zweite Argument ausgeführt, andernfalls wird das dritte Argument ausgeführt.

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

Hinweise

Die Type -Eigenschaft des resultierenden ConditionalExpression ist gleich der Type -Eigenschaft von ifTrue.

Weitere Informationen

Gilt für:

Condition(Expression, Expression, Expression, Type)

Quelle:
ConditionalExpression.cs
Quelle:
ConditionalExpression.cs
Quelle:
ConditionalExpression.cs

Erstellt eine ConditionalExpression, die eine bedingte Anweisung darstellt.

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

Parameter

test
Expression

Ein Expression, auf den die Test-Eigenschaft festgelegt werden soll.

ifTrue
Expression

Ein Expression, auf den die IfTrue-Eigenschaft festgelegt werden soll.

ifFalse
Expression

Ein Expression, auf den die IfFalse-Eigenschaft festgelegt werden soll.

type
Type

Ein Type, auf das die Type-Eigenschaft festgelegt werden soll.

Gibt zurück

Ein ConditionalExpression, bei dem die NodeType-Eigenschaft gleich Conditional ist und die Eigenschaften Test, IfTrue und IfFalse auf die angegebenen Werte festgelegt sind.

Hinweise

Diese Methode ermöglicht die explizite Vereinheitlichung des Ergebnistyps des bedingten Ausdrucks in Fällen, in denen die Typen von ifTrue ausdrücken und ifFalse nicht gleich sind. Typen von und ifTrueifFalse müssen implizit auf den Ergebnistyp verweisen können. Der type darf sein Void.

Gilt für: