Expression.Condition 方法

定義

建立表示條件陳述式的 ConditionalExpression

多載

Condition(Expression, Expression, Expression)

建立表示條件陳述式的 ConditionalExpression

Condition(Expression, Expression, Expression, Type)

建立表示條件陳述式的 ConditionalExpression

Condition(Expression, Expression, Expression)

來源:
ConditionalExpression.cs
來源:
ConditionalExpression.cs
來源:
ConditionalExpression.cs

建立表示條件陳述式的 ConditionalExpression

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

參數

test
Expression

要將 Expression 屬性設定為與之相等的 Test

ifTrue
Expression

要將 Expression 屬性設定為與之相等的 IfTrue

ifFalse
Expression

要將 Expression 屬性設定為與之相等的 IfFalse

傳回

ConditionalExpression,其 NodeType 屬性等於 Conditional,且 TestIfTrueIfFalse 屬性設定為指定的值。

例外狀況

testifTrueifFalsenull

test.Type 不是 Boolean

-或-

ifTrue.Type 不等於 ifFalse.Type。

範例

下列程式碼範例示範如何建立代表條件陳述式的運算式。 如果第一個引數評估為 true ,則會執行第二個引數,否則會執行第三個引數。

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

備註

Type產生的 ConditionalExpression 屬性等於 TypeifTrue 屬性。

另請參閱

適用於

Condition(Expression, Expression, Expression, Type)

來源:
ConditionalExpression.cs
來源:
ConditionalExpression.cs
來源:
ConditionalExpression.cs

建立表示條件陳述式的 ConditionalExpression

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

參數

test
Expression

要將 Expression 屬性設定為與之相等的 Test

ifTrue
Expression

要將 Expression 屬性設定為與之相等的 IfTrue

ifFalse
Expression

要將 Expression 屬性設定為與之相等的 IfFalse

type
Type

要將 Type 屬性設定為與之相等的 Type

傳回

ConditionalExpression,其 NodeType 屬性等於 Conditional,且 TestIfTrueIfFalse 屬性設定為指定的值。

備註

這個方法允許在 和 ifFalse 運算式的類型不相等的情況下,明確統一條件運算式的結果類型 ifTrue 。 和 ifFalse 的類型 ifTrue 都必須隱含參考可指派給結果類型。 允許為 typeVoid

適用於