Expression.Condition Metoda

Definicja

Tworzy obiekt ConditionalExpression , który reprezentuje instrukcję warunkową.

Przeciążenia

Condition(Expression, Expression, Expression)

Tworzy obiekt ConditionalExpression , który reprezentuje instrukcję warunkową.

Condition(Expression, Expression, Expression, Type)

Tworzy obiekt ConditionalExpression , który reprezentuje instrukcję warunkową.

Condition(Expression, Expression, Expression)

Źródło:
ConditionalExpression.cs
Źródło:
ConditionalExpression.cs
Źródło:
ConditionalExpression.cs

Tworzy obiekt ConditionalExpression , który reprezentuje instrukcję warunkową.

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

Parametry

test
Expression

Element Expression do ustawiania Test właściwości równej.

ifTrue
Expression

Element Expression do ustawiania IfTrue właściwości równej.

ifFalse
Expression

Element Expression do ustawiania IfFalse właściwości równej.

Zwraca

Właściwość ConditionalExpression , która ma właściwość równą ConditionalNodeType i Test, IfTruei właściwości IfFalse ustawione na określone wartości.

Wyjątki

test lub ifTrueifFalse ma wartość null.

test. Typ nie Booleanjest .

-lub-

ifTrue. Typ nie jest równy ifFalse. Typu.

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć wyrażenie reprezentujące instrukcję warunkową. Jeśli pierwszy argument zwróci wartość true, drugi argument jest wykonywany. W przeciwnym razie jest wykonywany trzeci argument.

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

Uwagi

Właściwość Type wynikowej ConditionalExpression jest równa Type właściwości .ifTrue

Zobacz też

Dotyczy

Condition(Expression, Expression, Expression, Type)

Źródło:
ConditionalExpression.cs
Źródło:
ConditionalExpression.cs
Źródło:
ConditionalExpression.cs

Tworzy obiekt ConditionalExpression , który reprezentuje instrukcję warunkową.

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

Parametry

test
Expression

Element Expression do ustawiania Test właściwości równej.

ifTrue
Expression

Element Expression do ustawiania IfTrue właściwości równej.

ifFalse
Expression

Element Expression do ustawiania IfFalse właściwości równej.

type
Type

A Type , aby ustawić właściwość równą Type .

Zwraca

Właściwość ConditionalExpression , która ma właściwość równą ConditionalNodeType i Test, IfTruei właściwości IfFalse ustawione na określone wartości.

Uwagi

Ta metoda umożliwia jawne ujednolicenie typu wyniku wyrażenia warunkowego w przypadkach, gdy typy ifTrue wyrażeń i ifFalse nie są równe. Typy obu ifTrue typów i ifFalse muszą być niejawnie przywoływalne do typu wyników. Wartość type może mieć wartość Void.

Dotyczy