Expression.Condition Expression.Condition Expression.Condition Expression.Condition Method

定義

条件付きステートメントを表す ConditionalExpression を作成します。Creates a ConditionalExpression that represents a conditional statement.

オーバーロード

Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression)

条件付きステートメントを表す ConditionalExpression を作成します。Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression, Type) Condition(Expression, Expression, Expression, Type) Condition(Expression, Expression, Expression, Type)

条件付きステートメントを表す ConditionalExpression を作成します。Creates a ConditionalExpression that represents a conditional statement.

Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression) Condition(Expression, Expression, Expression)

条件付きステートメントを表す ConditionalExpression を作成します。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

パラメーター

test
Expression Expression Expression Expression

Expression プロパティを等しく設定する TestAn Expression to set the Test property equal to.

ifTrue
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfTrueAn Expression to set the IfTrue property equal to.

ifFalse
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfFalseAn Expression to set the IfFalse property equal to.

戻り値

ConditionalExpression と等しい NodeType プロパティと、指定した値に設定された ConditionalTest、および IfTrue の各プロパティを含む IfFalseA ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

例外

testifTrue、または ifFalse は、null です。test or ifTrue or ifFalse is null.

test.Type が Boolean ではありません。test.Type is not Boolean.

または-or- ifTrue.Type が、ifFalse.Type と等しくありません。ifTrue.Type is not equal to ifFalse.Type.

次のコード例では、条件付きステートメントを表す式を作成する方法を示します。The following code example shows how to create an expression that represents a conditional statement. 最初の引数がと評価trueされた場合、2番目の引数が実行されます。それ以外の場合は、3番目の引数が実行されます。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

注釈

結果として得ConditionalExpressionられるのType ifTrue Typeプロパティは、のプロパティと同じです。The Type property of the resulting ConditionalExpression is equal to the Type property of ifTrue.

こちらもご覧ください

Condition(Expression, Expression, Expression, Type) Condition(Expression, Expression, Expression, Type) Condition(Expression, Expression, Expression, Type)

条件付きステートメントを表す ConditionalExpression を作成します。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

パラメーター

test
Expression Expression Expression Expression

Expression プロパティを等しく設定する TestAn Expression to set the Test property equal to.

ifTrue
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfTrueAn Expression to set the IfTrue property equal to.

ifFalse
Expression Expression Expression Expression

Expression プロパティを等しく設定する IfFalseAn Expression to set the IfFalse property equal to.

type
Type Type Type Type

Type プロパティを等しく設定する TypeA Type to set the Type property equal to.

戻り値

ConditionalExpression と等しい NodeType プロパティと、指定した値に設定された ConditionalTest、および IfTrue の各プロパティを含む IfFalseA ConditionalExpression that has the NodeType property equal to Conditional and the Test, IfTrue, and IfFalse properties set to the specified values.

注釈

このメソッドでは、式とifTrue ifFalse式の型が等しくない場合に、条件式の結果型を明示的に統合できます。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. ifTrueifFalseの両方の型は、暗黙的に結果の型に割り当て可能である必要があります。Types of both ifTrue and ifFalse must be implicitly reference assignable to the result type. はにすることVoidができます。typeThe type is allowed to be Void.

適用対象