Expression.Condition Método
Definição
Cria um ConditionalExpression que representa uma instrução condicional.Creates a ConditionalExpression that represents a conditional statement.
Sobrecargas
| Condition(Expression, Expression, Expression) |
Cria um ConditionalExpression que representa uma instrução condicional.Creates a ConditionalExpression that represents a conditional statement. |
| Condition(Expression, Expression, Expression, Type) |
Cria um ConditionalExpression que representa uma instrução condicional.Creates a ConditionalExpression that represents a conditional statement. |
Condition(Expression, Expression, Expression)
Cria um ConditionalExpression que representa uma instrução condicional.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
Parâmetros
- test
- Expression
Um Expression para definir a propriedade Test igual a ele.An Expression to set the Test property equal to.
- ifTrue
- Expression
Um Expression para definir a propriedade IfTrue igual a ele.An Expression to set the IfTrue property equal to.
- ifFalse
- Expression
Um Expression para definir a propriedade IfFalse igual a ele.An Expression to set the IfFalse property equal to.
Retornos
Um ConditionalExpression que tem a propriedade NodeType igual a Conditional e as propriedades Test, IfTrue e IfFalse definidas com os valores especificados.A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.
Exceções
test, ifTrue ou ifFalse é null.test or ifTrue or ifFalse is null.
test.Type não é Boolean.test.Type is not Boolean.
- ou --or-
ifTrue.Type não é igual a ifFalse.Type.ifTrue.Type is not equal to ifFalse.Type.
Exemplos
O exemplo de código a seguir mostra como criar uma expressão que representa uma instrução condicional.The following code example shows how to create an expression that represents a conditional statement. Se o primeiro argumento for avaliado como true , o segundo argumento será executado; caso contrário, o terceiro argumento será executado.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
Comentários
A Type Propriedade do resultante ConditionalExpression é igual à Type propriedade de ifTrue .The Type property of the resulting ConditionalExpression is equal to the Type property of ifTrue.
Confira também
Aplica-se a
Condition(Expression, Expression, Expression, Type)
Cria um ConditionalExpression que representa uma instrução condicional.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
Parâmetros
- test
- Expression
Um Expression para definir a propriedade Test igual a ele.An Expression to set the Test property equal to.
- ifTrue
- Expression
Um Expression para definir a propriedade IfTrue igual a ele.An Expression to set the IfTrue property equal to.
- ifFalse
- Expression
Um Expression para definir a propriedade IfFalse igual a ele.An Expression to set the IfFalse property equal to.
- type
- Type
Um Type para definir a propriedade Type igual a ele.A Type to set the Type property equal to.
Retornos
Um ConditionalExpression que tem a propriedade NodeType igual a Conditional e as propriedades Test, IfTrue e IfFalse definidas com os valores especificados.A ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.
Comentários
Esse método permite unificar explicitamente o tipo de resultado da expressão condicional em casos em que os tipos ifTrue de ifFalse expressões e não são iguais.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. Os tipos de ifTrue e ifFalse devem ser implicitamente referenciados como atribuíveis ao tipo de resultado.Types of both ifTrue and ifFalse must be implicitly reference assignable to the result type. O type pode ser Void .The type is allowed to be Void.