Expression.Condition Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Crea un oggetto ConditionalExpression che rappresenta un'istruzione condizionale.
Overload
| Condition(Expression, Expression, Expression) |
Crea un oggetto ConditionalExpression che rappresenta un'istruzione condizionale. |
| Condition(Expression, Expression, Expression, Type) |
Crea un oggetto ConditionalExpression che rappresenta un'istruzione condizionale. |
Condition(Expression, Expression, Expression)
- Origine:
- ConditionalExpression.cs
- Origine:
- ConditionalExpression.cs
- Origine:
- ConditionalExpression.cs
Crea un oggetto ConditionalExpression che rappresenta un'istruzione condizionale.
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
Parametri
- test
- Expression
Oggetto Expression su cui impostare la proprietà Test.
- ifTrue
- Expression
Oggetto Expression su cui impostare la proprietà IfTrue.
- ifFalse
- Expression
Oggetto Expression su cui impostare la proprietà IfFalse.
Restituisce
Oggetto ConditionalExpression la cui proprietà NodeType è uguale a Conditional e le cui proprietà Test, IfTrue e IfFalse sono impostate sui valori specificati.
Eccezioni
Il parametro test, il parametro ifTrue o il parametro ifFalse è null.
Esempio
Nell'esempio di codice seguente viene illustrato come creare un'espressione che rappresenta un'istruzione condizionale. Se il primo argomento restituisce true, viene eseguito il secondo argomento. In caso contrario, viene eseguito il terzo argomento.
// 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
Commenti
La Type proprietà del risultato ConditionalExpression è uguale alla Type proprietà di ifTrue.
Vedi anche
Si applica a
Condition(Expression, Expression, Expression, Type)
- Origine:
- ConditionalExpression.cs
- Origine:
- ConditionalExpression.cs
- Origine:
- ConditionalExpression.cs
Crea un oggetto ConditionalExpression che rappresenta un'istruzione condizionale.
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
Parametri
- test
- Expression
Oggetto Expression su cui impostare la proprietà Test.
- ifTrue
- Expression
Oggetto Expression su cui impostare la proprietà IfTrue.
- ifFalse
- Expression
Oggetto Expression su cui impostare la proprietà IfFalse.
Restituisce
Oggetto ConditionalExpression la cui proprietà NodeType è uguale a Conditional e le cui proprietà Test, IfTrue e IfFalse sono impostate sui valori specificati.
Commenti
Questo metodo consente di unificare in modo esplicito il tipo di risultato dell'espressione condizionale nei casi in cui i tipi di ifTrue espressioni e ifFalse non sono uguali. I tipi di entrambi ifTrue e ifFalse devono essere in modo implicito riferimenti assegnabili al tipo di risultato. L'oggetto type può essere Void.