Expression.Condition 方法

定义

创建一个表示条件语句的 ConditionalExpression

重载

Condition(Expression, Expression, Expression)

创建一个表示条件语句的 ConditionalExpression

Condition(Expression, Expression, Expression, Type)

创建一个表示条件语句的 ConditionalExpression

Condition(Expression, Expression, Expression)

创建一个表示条件语句的 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

一个 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属性等于 Type . 的属性ifTrue

另请参阅

适用于

Condition(Expression, Expression, Expression, Type)

创建一个表示条件语句的 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

一个 ConditionalExpression,其 NodeType 属性等于 Conditional,并且其 TestIfTrueIfFalse 属性设置为指定值。

注解

此方法允许在条件表达式的类型和ifFalse表达式不相等的情况下显式统一条件表达式的结果类型ifTrue。 这两ifTrue``ifFalse者的类型必须隐式引用才能分配给结果类型。 允许该属性 typeVoid.

适用于